在configure(HttpSecurity http)方法中加入
http.exceptionHandling().accessDeniedPage("/error.html");//配置没有权限访问跳转的自定义页面
整体方法如下
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(password());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling().accessDeniedPage("/error.html");//配置没有权限访问跳转的自定义页面
http.formLogin() //自定义编写的登录页面
.loginPage("/login.html")//登录页面设置
.loginProcessingUrl("/user/login")//登录后访问路径
.defaultSuccessUrl("/test/index")//登录成功后跳转路径
.and().authorizeRequests()
.antMatchers("/","/user/login").permitAll()//设置哪些路径可以直接访问,不需要认证
.antMatchers("/test/index").hasAnyRole("sale,master")//具有ROLE_sale,ROLE_master的角色可以访问这个路径
.anyRequest().authenticated()//除上面外的所有请求全部需要鉴权认证
.and().csrf().disable();//关闭csrf防护
}
@Bean
PasswordEncoder password()
{
return new BCryptPasswordEncoder();
}
}