太久没有发过博客,总结一波SpringSecurity登录认证,记录每次的学习体验。。。。。。。。。。
实现WebSecurityConfigurerAdapter 配置安全框架
//@EnableWebSecurity 的 JavaConfig 配置类到 IoC 容器
@EnableWebSecurity
// 启用方法级别的权限认证
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
【1】配置静态资源放行名单,可以省略。
在configure(httpSecurity http)配置静态资源放行路径 @Override public void configure(WebSecurity web) throws Exception { super.configure(web); }
【2】配置Security安全认证请求
@Override protected void configure(HttpSecurity http) throws Exception { super.configure(http); } 【3】身份认证接口 可以实现多种认证,如web登录认证,微信登录认证,APP认证。。。。。 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { super.configure(auth); } }
【1】配置静态资源放行名单,可以省略。
也可以在在configure(httpSecurity http)配置静态资源放行路径 //放行静态资源 web.ignoring().antMatchers(HttpMethod.GET, "/static/**", "/PearAdmin/**", "/**/*.html", "/**/*.css", "/**/*.js");
【2】配置Security安全认证请求
(1)配置通用
//禁用页面镶嵌frame劫持安全协议 // 防止iframe 造成跨域 http.headers().frameOptions().disable(); //关闭csrf 跨站请求防护 http.csrf().disable(); // 禁用缓存 http.headers().cacheControl(); //开启自动配置的注销功能注销成功以后来到首页 http.logout().logoutSuccessUrl("/admin/login"); //无权访问 JSON 格式的数据 http.exceptionHandling()
//当访问接口没有权限触发响应事件
.accessDeniedHandler(accessDeniedHandler