<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 创建一个MyUserDatailService 这里提供了登录数据,每当有登录时 会调用 这个类 的loadUserByUsername 方法
@Service
public class MyUserDatailService implements UserDetailsService{
@Autowired
UserService service;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_" + "ADMIN"));
return new User(
username,
passwordEncoder.encode("admin"),
authorities
);
}
}
public class OnSucessHandle implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.getWriter().write("登录成功");
}
}
public class OnFaildHandle implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
response.getWriter().write("登录失败");
}
}
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDatailService userDatailService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(new VerCodeFilter("/Login/Login"), UsernamePasswordAuthenticationFilter.class);
http.formLogin().
loginProcessingUrl("/Login/Login")
.successHandler(new OnSucessHandle())
.failureHandler(new OnFaildHandle()).
and()
.authorizeRequests()
.antMatchers("/Login/**").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.
userDetailsService(userDatailService)
.passwordEncoder(new BCryptPasswordEncoder());
}
}
public class VerCodeFilter extends AbstractAuthenticationProcessingFilter {
private final String LoginURL="/Login/Login";
public VerCodeFilter(String defaultFilterProcessesUrl) {
super(defaultFilterProcessesUrl);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res=(HttpServletResponse)response;
if(LoginURL.equals(req.getServletPath()) && "POST".equalsIgnoreCase(req.getMethod())){
HttpSession session = req.getSession();
if (session.getAttribute("code")==null ||!session.getAttribute("code").equals(req.getParameter("Code"))){
res.getWriter().write("验证码错误");
return;
}
}
chain.doFilter(request, response);
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
return null;
}
}
@PreAuthorize("hasAnyRole('ADMIN')")
@GetMapping("/queryUserPage")
public String getTest(){
return "asdasd";
}