springboot 整合 springsecurity

  • spring-boot 整合 spring-security

    • maven 支持

              <!-- spring security 认证授权 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
      </dependency>
      
    • 配置文件

      @EnableWebSecurity
      public class MultiHttpSecurityConfig {
      
          @Configuration
          public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
      
              // 静态资源访问的 url
              private String[] staticFileUrl = {};
              // 不用认证就可访问的 url
              private String[] permitUrl = {};
      
              @Override
              public void configure(WebSecurity web) throws Exception {
                  web.ignoring().antMatchers(staticFileUrl);
                  web.ignoring().antMatchers(permitUrl);
              }
      
              @Override
              protected void configure(HttpSecurity http) throws Exception {
                  // 访问url认证
                  http
                          .authorizeRequests()
                          .antMatchers("/admin/**").hasAuthority(String.valueOf(AuthorityName.ROLE_ADMIN))
                          .anyRequest().authenticated();
                  // 配置登陆信息
                  http
                          .formLogin().loginPage("/login")
                          .defaultSuccessUrl("/goIndex")
                          .permitAll()
                          .and();
                  // 配置退出登陆信息
                  http
                          .logout()
                          .logoutSuccessUrl("/login")
                          .invalidateHttpSession(true)
                          .deleteCookies()
                          .and();
                  http.httpBasic();
              }
          }
      }
      
    • 使用 自己数据库的 用户信息,进行对登陆的form提交的信息,进行验证。验证成功后为该用户配置相应的权限。

      @Service
      public class UserDetailsServiceImpl implements UserDetailsService {
      
          @Autowired
          private UserRepository userRepository;
      
          @Override
          public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
              User user = userRepository.findByUserName(username);
      
              if (user == null) {
                  throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
              } else {
                  return JwtUserFactory.create(user);
              }
          }
      }
      
    • 注意:

      • 需要实现该接口:
        import org.springframework.security.core.userdetails.UserDetailsService;
      • 并实现方法:UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
    • 至此,springboot 整合 springsecurity 已经完成,不过,对于权限认证,使用的是 form 表单提交登陆的方式。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,010评论 6 342
  • =========================================================...
    lavor阅读 3,541评论 0 5
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,877评论 18 399
  • 一谈到仪式,我就想到“套路”、“形式主义”,我打心底里是拒绝的。但有两种仪式,我一定会抽时间去参加——婚礼和葬礼。...
    李明_日拱一卒阅读 874评论 0 3