HttpSecurity(接口权限管理)
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("login.html")
.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)
.addLogoutHandler(new ExitHandler())
.deleteCookies("JSESSIONID","token")
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.maximumSessions(1)
.maxSessionsPreventsLogin(false)
.expiredUrl("/login")
.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失效处理 |
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
可以通过以下选项准确控制会话何时创建以及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 {
auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
}
WebSecurity(静态资源管理)
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/images/**","/static/**", "/css/**", "/fonts/**", "/img/**", "/js/**");
}