spring boot + spring security
之前没有遇到过这个问题,今天晚上登录的时候突发,本人刚入门spring boot 求大佬赐教
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Bean
UserDetailsService customUserService(){ //CustomUserService注册bean
return new CustomUserService();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserService()); //添加自定义认证
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.disable();
http.authorizeRequests()
.antMatchers("/js/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/bootstrap/**").permitAll()
.antMatchers("/fonts/**").permitAll()
.antMatchers("/favicon.ico").permitAll()
.anyRequest().authenticated() //4
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/word")
.failureUrl("/login?error")
.permitAll() //登陆页面可任意访问
.and()
.logout().permitAll(); //注销页面可任意访问
}
}
public class WebMvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");//注册访问/login转向login.html页面
}
}
控制层:
@Controller
public class HomeController {
@RequestMapping("/word")
public String index(Model model){
return "home";
}
@RequestMapping(value="/login",method = RequestMethod.GET)
public String login(){
return "login";
}
}