| | |
| | | import com.google.code.kaptcha.Producer; |
| | | import com.google.code.kaptcha.impl.DefaultKaptcha; |
| | | import com.google.code.kaptcha.util.Config; |
| | | import com.ks.daylucky.util.Constant; |
| | | import com.ks.lucky.pojo.DO.LuckySponsors; |
| | | import com.ks.lucky.remote.service.LuckySponsorService; |
| | | import net.sf.json.JSONObject; |
| | | import org.apache.dubbo.config.annotation.Reference; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.context.annotation.Bean; |
| | |
| | | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.AuthenticationException; |
| | | import org.springframework.security.core.GrantedAuthority; |
| | | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| | | import org.springframework.security.core.userdetails.UserDetails; |
| | | import org.springframework.security.core.userdetails.UserDetailsService; |
| | | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Properties; |
| | | |
| | | @EnableWebSecurity |
| | | public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
| | | |
| | | @Reference(version = "1.0.0") |
| | | private LuckySponsorService luckySponsorService; |
| | | |
| | | private Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class); |
| | | |
| | | |
| | | private final String LOGIN_PROCESSING_URL="/admin/api/login"; |
| | | private final String LOGIN_PROCESSING_URL = "/admin/api/user/login"; |
| | | |
| | | //图形验证码配置 |
| | | @Bean |
| | |
| | | http.headers().frameOptions().disable(); |
| | | http.authorizeRequests() |
| | | .antMatchers("/admin/api/captcha.jpg*").permitAll() |
| | | .antMatchers("/admin/api/**","/index.html").authenticated() |
| | | .antMatchers("/admin/api/**", "/index.html").authenticated() |
| | | .and() |
| | | .formLogin() |
| | | //自定义登录界面 |
| | | .loginPage("/login.html") |
| | | .loginPage("/admin/login.html") |
| | | //设置接收的属性字段 |
| | | .usernameParameter("account") |
| | | .passwordParameter("pwd") |
| | | //处理登录逻辑的url |
| | | .loginProcessingUrl(LOGIN_PROCESSING_URL) |
| | | //登录成功后的跳转 |
| | | .successHandler(new AuthenticationSuccessHandler() { |
| | | @Override |
| | | public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { |
| | | SecurityUser user = (SecurityUser) authentication.getPrincipal(); |
| | | LuckySponsors sponsors =user.getSponsors(); |
| | | httpServletRequest.getSession().setAttribute(Constant.SESSION_ADMIN_SPONSOR_KEY, sponsors); |
| | | logger.info("successHandler"); |
| | | httpServletResponse.setContentType("application/json;charset=UTF-8"); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("code", 200); |
| | | jsonObject.put("code", 0); |
| | | jsonObject.put("msg", "登录成功"); |
| | | httpServletResponse.getWriter().print(jsonObject); |
| | | } |
| | |
| | | |
| | | } |
| | | |
| | | class SecurityUser implements UserDetails { |
| | | |
| | | private LuckySponsors sponsors; |
| | | |
| | | public SecurityUser() { |
| | | |
| | | } |
| | | |
| | | public SecurityUser(LuckySponsors sponsors) { |
| | | this.sponsors = sponsors; |
| | | } |
| | | |
| | | public LuckySponsors getSponsors() { |
| | | return sponsors; |
| | | } |
| | | |
| | | @Override |
| | | public Collection<? extends GrantedAuthority> getAuthorities() { |
| | | Collection<GrantedAuthority> authorities = new ArrayList<>(); |
| | | SimpleGrantedAuthority authority = new SimpleGrantedAuthority("admin"); |
| | | authorities.add(authority); |
| | | return authorities; |
| | | } |
| | | |
| | | @Override |
| | | public String getPassword() { |
| | | return sponsors.getPwd(); |
| | | } |
| | | |
| | | @Override |
| | | public String getUsername() { |
| | | return sponsors.getName(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean isAccountNonExpired() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isAccountNonLocked() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isCredentialsNonExpired() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isEnabled() { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | class MyUserDetailsService implements UserDetailsService { |
| | | |
| | | @Override |
| | | public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { |
| | | |
| | | LuckySponsors sponsors = luckySponsorService.getSponsorByAccount(s); |
| | | if (sponsors == null) { |
| | | throw new UsernameNotFoundException("账户不存在"); |
| | | } |
| | | //TODO 用户权限赋予 |
| | | return null; |
| | | return new SecurityUser(sponsors); |
| | | } |
| | | } |
| | | |
| | |
| | | throw new BadCredentialsException("密码错误"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| | | // 获取前端表单中输入后返回的用户名、密码 |
| | | String userName = (String) authentication.getPrincipal(); |
| | | String password = StringUtil.Md5((String) authentication.getCredentials()); |
| | | |
| | | SecurityUser userInfo = (SecurityUser) this.getUserDetailsService().loadUserByUsername(userName); |
| | | |
| | | boolean isValid = password.equalsIgnoreCase(userInfo.getPassword()); |
| | | // 验证密码 |
| | | if (!isValid) { |
| | | throw new BadCredentialsException("密码错误!"); |
| | | } |
| | | return new UsernamePasswordAuthenticationToken(userInfo, password, userInfo.getAuthorities()); |
| | | } |
| | | } |
| | | } |