URL匹配
.and().authorizeRequests()拼接URL权限配置
URL匹配
requestMatchers():配置一个request Mather数组,参数为RequestMatcher 对象,其match 规则自定义,需要的时候放在最前面,对需要匹配的的规则进行自定义与过滤antMatchers():配置一个request Mather 的 string数组,参数为 ant 路径格式, 直接匹配url
-anyRequest: 匹配任意url,无参 ,最好放在最后面
其他
authenticated(): 保护UrL,需要用户登录permitAll(): 指定URL无需保护hasAuthority(String authority):传入单个权限,有该权限的用户才能访问该url。hasAnyAuthority(String… authorities):传入多个权限,登录用户有其中某一个权限,就能访问该url。access(String attribute): 该方法使用 SPEL, 所以可以创建复杂的限制 例如如access(“permitAll”), - access(“hasRole(‘ADMIN’) and hasIpAddress(‘123.123.123.123’)”)hasIpAddress(String ipaddressExpression): 限制IP地址或子网
特别注意:
hasRole(String role): 传入单个角色,有该角色的用户才能访问该url。
传入的角色字符串,如果没有前缀ROLE_,就在前面拼装ROLE_,所以hasRole("ADMIN")等同于hasRole("ROLE_ADMIN")hasAnyRole(String… roles): 传入多个角色,登录用户有其中某一个角色,就能访问该url。
传入的角色字符串,如果没有前缀ROLE_,就在前面拼装ROLE_,所以hasAnyRole("ADMIN","HR")等同于hasAnyRole("ROLE_ADMIN","ROLE_HR")
原理如下:https://www.malaoshi.top/show_1EF60v8dUrCp.html
登录login
.and().formLogin(): 拼接表单登录部分loginPage(): 登录页usernameParameter(String usernameParameter)登录页面用户名控件name值,默认 username 。passwordParameter(String passwordParameter)登录页面密码控件name值,默认 password
defaultSuccessUrl: 登录成功后的默认处理页successForwardUrl("/index.html")登录成功后跳转的页面,一般前后分离用到该功能。可以转发Controller,但不能转发到页面
failuerHandler:登录失败之后的处理器successHandler:登录成功之后的处理器
failuerUrl:登录失败之后系统转向的url,默认是this.loginPage + “?error”failureForwardUrl(String forwardUrl)登录失败转发url, 一般前后分离用到该功能。可以转发Controller,但不能转发到页面permitAll():login块的URL无需保护
注销logout
.and().logout()拼接logout部分logoutUrl: 登出url , 默认是/logout, 它可以是一个ant path urllogoutSuccessUrl("/login.html"): 登出成功后跳转的 url 默认是”/login?logout”logoutSuccessHandler: 登出成功处理器,设置后会把logoutSuccessUrl 置为nullinvalidateHttpSession(true)清空sessionpermitAll(): logout块的URL无需保护
session管理
.and().sessionManagement()拼接session管理部分invalidSessionUrl("/login.html")设置Session失效跳转页面maximumSessions(1)设置最大Session数为1,同一个用户登录第二次时,第一次被踢出expiredSessionStrategy()设置Session过期处理策略
例子
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true,prePostEnabled = true,jsr250Enabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// 安全拦截机制
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
// .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
// .and()
.authorizeRequests()//url权限配置
// .antMatchers("/index.html").permitAll() // 匹配这些路径可以所有人访问
// .antMatchers("/dept/**").hasRole("admin")
.antMatchers("/user/**").hasRole("admin")
.anyRequest().authenticated()
.and()
//login部分
.formLogin()
.loginPage("/login.html")//指定登录页面。所有url前面必须写/,否则报错 'xxx' is not a valid forward URL
// .usernameParameter("user")//登录页面用户名控件name值
// .passwordParameter("pwd")//登录页面密码控件name值
.loginProcessingUrl("/login")//security提供的处理登录url,默认是/login
// .successForwardUrl("/index.html")//登录成功后跳转的页面,需要额外处理才能使用,否则页面会提示错误
.defaultSuccessUrl("/index.html")//登录成功后重定向的页面
// .failureUrl("/login_fails.html")//登录失败跳转的页面
.failureForwardUrl("/loginError")//登录失败转发的url,可以是Controller,但不能是页面,页面不会转发
.permitAll()//让login部分的url可以任意访问
//
// //logout部分
.and()
.logout()//退出登录需要提交post请求
.logoutSuccessUrl("/login.html")//退出成功访问的页面
.invalidateHttpSession(true)//清空session
.permitAll();//让logout部分的url可以任意访问
}
}
感谢:
https://www.jianshu.com/p/e3a9a8c4876c