SpringSecurity--SpringBoot基本配置

旧城等待, 2021-08-24 14:43 659阅读 0赞

SpringSecurity的基本配置

SpringBoot针对SpringSecurity提供了自动化配置方案,因此可以使SpringSecurity非常容易地整合进SpringBoot项目中,这也是在SpringBoot项目中使用SpringSecurity的优势。

基本用法

  1. 创建项目,添加依赖:


    org.springframework.boot
    spring-boot-starter-security


    org.springframework.boot
    spring-boot-starter-web
  2. 添加hello接口:

    @RestController
    public class HelloController {

    1. @GetMapping("/hello")
    2. public String hello(){
    3. return "Hello";
    4. }

    }

  3. 启动项目测试:
    访问/hello接口会自动跳转到登录页面,这个登录页面是由SpringSecutiry提供的:
    在这里插入图片描述

默认的用户名是user,默认的登录密码则在每次启动项目时随机生成:
在这里插入图片描述

配置用户名和密码

可以在application.properties中配置默认的用户名、密码以及用户角色:

  1. spring.security.user.name=sang
  2. spring.security.user.password=123
  3. spring.security.user.roles=admin

当开发者在application.properties中配置了默认的用户名和密码后,再次启动项目,项目启动日志就不会打印出随机生成的密码了,用户可直接使用配置好的用户名和密码登录,登录成功后,用户还具有一个角色——admin。

基于内存的认证

开发者也可以自定义类继承自WebSecurityConfigurerAdapter,进而实现对SpringSecurity更多的自定义配置,例如基于内存的认证,配置方式如下:

  1. //自定义MyWebSecurityConfig继承WebSecurityConfigurerAdapter
  2. @Configuration
  3. public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
  4. //指定不对密码进行加密
  5. @Bean
  6. PasswordEncoder passwordEncoder(){
  7. return NoOpPasswordEncoder.getInstance();
  8. }
  9. /* 重写configure(AuthenticationManagerBuilder auth) 配置两个用户: 1. 用户名:admin,密码:123,角色:ADMIN和USER 2. 用户名:sang,密码:123,角色:USER */
  10. @Override
  11. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  12. auth.inMemoryAuthentication()
  13. .withUser("admin").password("123").roles("ADMIN", "USER")
  14. .and()
  15. .withUser("sang").password("123").roles("USER");
  16. }
  17. }

配置完成后,重启项目,就可以使用这里配置的两个用户进行登录了。

HttpSecurity

虽然现在可以实现认证功能,但是受保护的资源都是默认的,而且也不能根据实际情况进行角色管理,如果要实现这些功能,就需要重写WebSecurityConfigurerAdapter中的另一个方法:

  1. @Configuration
  2. public class MyWebSecurityConfig2 extends WebSecurityConfigurerAdapter {
  3. @Bean
  4. PasswordEncoder passwordEncoder(){
  5. return NoOpPasswordEncoder.getInstance();
  6. }
  7. /* 配置三个用户: 1. root用户具备ADMIN和DBA角色 2. admin具备ADMIN和USER角色 3. sang具备USER角色 */
  8. @Override
  9. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  10. auth.inMemoryAuthentication()
  11. .withUser("root").password("123").roles("ADMIN", "DBA")
  12. .and()
  13. .withUser("admin").password("123").roles("ADMIN", "USER")
  14. .and()
  15. .withUser("sang").password("123").roles("USER");
  16. }
  17. /* 1. 用户访问"/admin/**"必须具备ADMIN角色 2. 用户访问"/user/**"必须具备ADMIN或者USER角色 3. 用户访问"/db/**"必须具备ADMIN和DBA的角色 */
  18. @Override
  19. protected void configure(HttpSecurity http) throws Exception {
  20. //开启HttpSecurity的配置
  21. http.authorizeRequests()
  22. .antMatchers("/admin/**")
  23. .hasRole("ADMIN")
  24. .antMatchers("/user/**")
  25. .access("hasAnyRole('ADMIN', 'USER')")
  26. .antMatchers("/db/**")
  27. .access("hasRole('ADMIN') and hasRole('DBA')")
  28. .anyRequest()//除了前面定义的URL模式之外,用户访问其它的URL都必须认证后访问
  29. .authenticated()
  30. .and()
  31. .formLogin()//开启表单登录,配置登录接口为"/login",可以直接调用"/login"接口,发起一个POST请求进行登录,参数中用户名必须为username,密码必须为password
  32. .loginProcessingUrl("/login")
  33. .permitAll()//和登录相关的接口都不要认证即可访问
  34. .and()
  35. .csrf()//关闭csrf
  36. .disable();
  37. }
  38. }

在Controller添加如下接口进行测试:

  1. @RestController
  2. public class HelloController {
  3. @GetMapping("/hello")
  4. public String hello(){
  5. return "Hello";
  6. }
  7. @GetMapping("/admin/hello")
  8. public String admin(){
  9. return "hello admin!";
  10. }
  11. @GetMapping("/user/hello")
  12. public String user(){
  13. return "hello user!";
  14. }
  15. @GetMapping("/db/hello")
  16. public String dba(){
  17. return "hello dba!";
  18. }
  19. }

测试结果:

  • “/admin/hello”接口:root和admin用户具有访问权限
  • “/user/hello”接口:admin和sang用户具有访问权限
  • “/db/hello”接口:只有root用户具有访问权限

发表评论

表情:
评论列表 (有 0 条评论,659人围观)

还没有评论,来说两句吧...

相关阅读

    相关 OSPF基本配置

    简介:OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议(Interior Gateway Protocol,简称IGP),用于在

    相关 nginx基本配置

    最近因为web项目多点部署,需要用到反向代理,所以选择了现在和apache同样火热的nginx实践了一下,非常庞大的一个工具,还有很多功能等待使用和研究,以下是初步使用的记录过