SpringSecurity5(1-快速入门)

打印 上一主题 下一主题

主题 939|帖子 939|积分 2817

依靠
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-security</artifactId>
  4.     <version>2.3.12.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.springframework.boot</groupId>
  8.     <artifactId>spring-boot-starter-web</artifactId>
  9.     <version>2.3.12.RELEASE</version>
  10. </dependency>
复制代码
登录认证
  1. @Controller
  2. public class UserController {
  3.     @GetMapping("/test")
  4.     @ResponseBody
  5.     public String test(){
  6.         return "hello";
  7.     }
  8. }
复制代码
注意:导入依靠之后,访问 localhost: 8080/test 请求地址会自动跳转到 localhost: 8080/test 中,出现一个表单,需要登录后才能访问
用户名默认为 user,密码在控制台出现


将表单请求转换为弹出框请求

WebSecurityConfigurerAdapter
  1. /**
  2. * 定制用户认证管理器来实现用户认证
  3. *  1. 提供用户认证所需信息(用户名、密码、当前用户的资源权)
  4. *  2. 可采用内存存储方式,也可能采用数据库方式
  5. */
  6. void configure(AuthenticationManagerBuilder auth);
  7. /**
  8. * 定制基于 HTTP 请求的用户访问控制
  9. *  1. 配置拦截的哪一些资源
  10. *  2. 配置资源所对应的角色权限
  11. *  3. 定义认证方式:HttpBasic、HttpForm
  12. *  4. 定制登录页面、登录请求地址、错误处理方式
  13. *  5. 自定义 Spring Security 过滤器等
  14. */
  15. void configure(HttpSecurity http);
  16. /**
  17. * 定制一些全局性的安全配置,例如:不拦截静态资源的访问
  18. */
  19. void configure(WebSecurity web);
复制代码
使用案例

  • 创建配置类继承 WebSecurityConfigurerAdapter 类,实现 http 的 configure 方法
  1. @Configuration
  2. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3.     @Override
  4.     protected void configure(HttpSecurity http) throws Exception {
  5.         /**
  6.          * fromLogin():表单认证
  7.          * httpBasic():弹出框认证
  8.          * authorizeRequests():身份认证请求
  9.          * anyRequest():所有请求
  10.          * authenticated():身份认证
  11.          */
  12.         http.httpBasic()
  13.                 .and()
  14.                 .authorizeRequests()
  15.                 // 其它任何请求访问都需要先通过认证
  16.                 .anyRequest()
  17.                 .authenticated();
  18.     }
  19. }
复制代码

  • 访问地址:localhost: 8080/test,此时发现表单请求转换为弹出框请求

路径匹配器

MvcRequestMatcher

匹配规则


  • /a:仅匹配路径/a
  • /a/*:操作符* 会更换一个路径名。在这种情况下,它将匹配/a/b 或/a/c,而不是/a/b/c
  • /a/**:操作符** 会更换多个路径名。在这种情况下,/a 以及/a/b 和/a/b/c 都是这个表达式的匹配项
  • /a/{param}:这个表达式实用于具有给定路径参数的路径/a
  • /a/{param: regex}:只有当参数的值与给定正则表达式匹配时,此表达式才应用于具有给定路径参数的路径/a
使用案例


  • 单个请求无请求方法匹配
  1. http.authorizeRequests()
  2.     .mvcMatchers("/hello_user").hasRole("USER")
  3.     .mvcMatchers("/hello_admin").hasRole("ADMIN");
复制代码
假如使用脚色为“USER”的用户来访问“/hello_admin”端点,那么会出现克制访问的情况,由于“/hello_admin”端点只有脚色为“ADMIN”的用户才能访问
注意:没有被 MVC 匹配器所匹配的端点,其访问不受任何的限制,效果相称于如下所示的配置
  1. http.authorizeRequests()
  2.     .mvcMatchers("/hello_user").hasRole("USER")
  3.     .mvcMatchers("/hello_admin").hasRole("ADMIN");    .anyRequest().permitAll();
复制代码

  • 单个请求有请求方法匹配
假如一个 Controller 中存在两个路径完全一样的 HTTP 端点,可以把 HTTP 方法作为一个访问的维度进行控制
  1. http.authorizeRequests()
  2.     .mvcMatchers(HttpMethod.POST, "/hello").authenticated()
  3.     .mvcMatchers(HttpMethod.GET, "/hello").permitAll()
  4.     .anyRequest().denyAll();
复制代码

  • 多个路径匹配
  1. http.authorizeRequests()
  2.     .mvcMatchers("/test/xiao","/test/giao","/test/a","/test/a/b").hasRole("ADMIN");
  3. //可以简化为以下方式
  4. http.authorizeRequests()
  5.     .mvcMatchers("/test/**").hasRole("ADMIN");
复制代码

  • 带有路径变量匹配
  1. @GetMapping("/product/{code}")
  2. public String productCode(@PathVariable String code){
  3.     return code;
  4. }
复制代码
  1. http.authorizeRequests()
  2.     .mvcMatchers("/product/{code:^[0-9]*$}").permitAll();
复制代码
此时调用端点,假设 code = 1234a,不符合全部都是数字,报 401;然后再次调用端点,code = 12345,发现调用通过
AntPathRequestMatcher

Ant 匹配器的表现形式和使用方法与前面先容的 MVC 匹配器非常相似
使用方法:

  • antMatchers(String patterns)
  • antMatchers(HttpMethod method)
  • antMatchers(HttpMethod method, String patterns)
mvc 与 ant 匹配器的区别

  • antMatchers("/secured")仅仅匹配 /secured
  • mvcMatchers("/secured")匹配 /secured 之余还匹配 /secured/,/secured.html,/secured.xyz
因此 mvcMatcher 更加通用且容错性更高
RegexRequestMatcher

使用方法:

  • regexMatchers(HttpMethod method, String regex)
  • regexMatchers(String regex)
使用这一匹配器的主要优势在于它可以大概基于复杂的正则表达式对请求地址进行匹配,这是 MVC 匹配器和 Ant 匹配器无法实现的
  1. http.authorizeRequests()
  2.     //只有输入的请求是一个合法的邮箱地址才能允许访问
  3.     .regexMatchers("/email/{email:.*(.+@.+\\.com)}")
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表