HttpSecurity(接口权限管理)
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()//使用表单登录
.loginPage("login.html")//自定义请求页面
.permitAll()//声明该界面的权限(permitAll:任何人都可以访问)
.loginProcessingUrl("/web/user/login")//默认登录接口
.usernameParameter("username")//账号
.passwordParameter("password")//密码
.successHandler(new SuccessHandler())//登录成功处理方式
.failureHandler(new FailureHandler())//登录失败处理方式
.successForwardUrl("/index")//登录成功跳转页面
.defaultSuccessUrl("/index.html",true)//登录成功后跳转指定页面
.and()
.anthorizeRequests()
.anyRequest().authenticated()
.and().logout().logoutUrl()
.logoutSuccessHandler(new ExitSuccessHandler())//退出登录成功后执行方式
.clearAuthentication(true)//清除身份认证信息
.invalidateHttpSession(true)//使session失效
.addLogoutHandler(new ExitHandler())
.deleteCookies("JSESSIONID","token")//退出后删除指定cookie信息
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)//反之session固定攻击
.maximumSessions(1)//只允许N台设备同时在线(1台),需要重写用户实体类的hashCode()
.maxSessionsPreventsLogin(false)//false:允许后登陆的设备挤掉前面的设备;true:设备达到上限以后,不允许后面的设备登录
.expiredUrl("/login")//session失效返回路径
.expiredSessionStrategy(customExpiredSessionStrategy)
;
}
表单管理(formLogin)
方法名 | 参数类型 | 描述 |
loginPage() | String | 之定义默认跳转页面 |
permitAll() | / | 所有人都能访问 |
loginProcessingUrl | String | 自定义登录请求接口 |
usernameParameter | String | 执行身份验证时查找用户名的HTTP参数 |
passwordParameter | String | 执行身份验证时查找密码的HTTP参数 |
successHandler | AuthenticationSuccessHandler | 登录成功处理方式 |
failureHandler | AuthenticationFailureHandler | 登录失败处理方式 |
successForwardUrl | String | 转发身份验证成功处理程序 |
defaultSuccessUrl | String | 如果用户在验证之前没有访问过安全页面,则在验证成功后用户将被重定向到何处。这是一个捷径 |
认证请求(authorizeRequests)
方法名 | 参数类型 | 描述 |
antMatchers | String… | 指定接口赋予权限 |
permitAll | / | 所有权限 |
hasRole | String | 指定权限 |
access | String | 指定权限 |
会话管理
方法名 | 参数类型 | 描述 |
sessionCreationPolicy | SessionCreationPolicy | 防止session固定攻击 |
maximumSessions | int | 限制同时在线人数 |
maxSessionsPreventsLogin | boolean | 设备达到上限后,是否允许挤掉之前登录的设备:true:不允许;false:允许 |
expiredUrl | String | session失效返回路径 |
expiredSessionStrategy | CustomExpiredSessionStrategy | session失效处理 |
可以通过以下选项准确控制会话何时创建以及Spring Security如何与之交互:
机制 | 描述 |
ALWAYS | 如果没有session存在就创建一个 |
NEVER | SpringSecurity 将不会创建Session,但是如果应用中其他地方创建了Session,那么Spring Security将会使用它。 |
IF_REQUIRED | 如果需要就创建一个Session(默认)登录时 |
STATELESS | SpringSecurity将绝对不会创建Session,也不使用Session |
退出登录(logout)
方法名 | 参数类型 | 描述 |
logoutUrl | String | 配置退出登录路径 |
clearAuthentication | boolean | 是否清楚身份认证信息;true:是;false:否 |
invalidateHttpSession | boolean | 是否让session失效;true:是;false:否 |
deleteCookies | String… | 退出登录后清楚指定cookie信息 |
AuthenticationManagerBuilder(身份认证管理)
/**
* 身份认证管理
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// TODO Auto-generated method stub
auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
//super.configure(auth);
}
WebSecurity(静态资源管理)
/**
* 静态资源管理
*/
@Override
public void configure(WebSecurity web) throws Exception {
// TODO Auto-generated method stub
//web.ignoring().antMatchers("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.ico","/**/*.woff","/**/*.eot","/**/*.svg","/**/*.ttf");
web.ignoring().antMatchers("/images/**","/static/**", "/css/**", "/fonts/**", "/img/**", "/js/**");
//super.configure(web);
}