写在前面
  
  关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求。在Java EE领域,成熟的安全框架解决方案一般有 Apache Shiro、Spring Security等两种技术选型。Apache Shiro简单易用也算是一大优势,但其功能还是远不如 Spring Security强大。Spring Security可以为 Spring 应用提供声明式的安全访问控制,起通过提供一系列可以在 Spring应用上下文中可配置的Bean,并利用 Spring IoC和 AOP等功能特性来为应用系统提供声明式的安全访问控制功能,减少了诸多重复工作。
  
  关于JWT JSON Web Token (JWT),是在网络应用间传递信息的一种基于 JSON的开放标准((RFC 7519),用于作为JSON对象在不同系统之间进行安全地信息传输。主要使用场景一般是用来在 身份提供者和服务提供者间传递被认证的用户身份信息。关于JWT的科普,可以看看阮一峰老师的《JSON Web Token 入门教程》。
  
  本文则结合 Spring Security和 JWT两大利器来打造一个简易的权限系统。
  
  本文实验环境如下:
  
  Spring Boot版本:2.0.6.RELEASE
  
  IDE:IntelliJ IDEA 2018.2.4
  
  另外本文实验代码置于文尾,需要自取。
  
  设计用户和角色
  
  本文实验为了简化考虑,准备做如下设计:
  
  设计一个最简角色表role,包括角色ID和角色名称
  
  角色表
  
  设计一个最简用户表user,包括用户ID,用户名,密码
  
  用户表
  
  再设计一个用户和角色一对多的关联表user_roles 一个用户可以拥有多个角色 用户-角色对应表
  
  创建 Spring Security和 JWT加持的 Web工程
  
  pom.xml 中引入 Spring Security和 JWT所必需的依赖
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-security</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>io.jsonwebtoken</groupId>
  
  <artifactId>jjwt</artifactId>
  
  <version>0.9.0</version>
  
  </dependency>
  
  项目配置文件中加入数据库和 JPA等需要的配置
  
  server.port=9991
  
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  
  spring.datasource.url=jdbc:mysql://121.196.XXX.XXX:3306/spring_security_jwt?useUnicode=true&characterEncoding=utf-8
  
  spring.datasource.username=root
  
  spring.datasource.password=XXXXXX
  
  logging.level.org.springframework.security=info
  
  spring.jpa.hibernate.ddl-auto=update
  
  spring.jpa.show-sql=true
  
  spring.jackson.serialization.indent_output=true
  
  创建用户、角色实体
  
  用户实体 User:
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @Entity
  
  public class User implements UserDetails {
  
  @Id
  
  @GeneratedValue
  
  private Long id;
  
  private String username;
  
  private String password;
  
  @ManyToMany(cascade = {CascadeType.REFRESH},fetch = FetchType.EAGER)
  
  private List<Role> roles;
  
  ...
  
  // 下面为实现UserDetails而需要的重写方法!
  
  @Override
  
  public Collection<? extends GrantedAuthority> getAuthorities() {
  
  List<GrantedAuthority> authorities = new ArrayList<>();
  
  for (Role role : roles) {
  
  authorities.add( new SimpleGrantedAuthority( role.getName() ) );
  
  }
  
  return authorities;
  
  }
  
  ...
  
  }
  
  此处所创建的 User类继承了 Spring Security的 UserDetails接口,从而成为了一个符合 Security安全的用户,即通过继承 UserDetails,即可实现 Security中相关的安全功能。
  
  角色实体 Role:
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @Entity
  
  public class Role {
  
  @Id
  
  @GeneratedValue
  
  private Long id;
  
  private String name;
  
  ... // 省略 getter和 setter
  
  }
  
  创建JWT工具类
  
  主要用于对 JWT Token进行各项操作,比如生成Token、验证Token、刷新Token等
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @Component
  
  public class JwtTokenUtil implements Serializable {
  
  private static final long serialVersionUID = -5625635588908941275L;
  
  private static final String CLAIM_KEY_USERNAME = "sub";
  
  private static final String CLAIM_KEY_CREATED = "created";
  
  public String generateToken(UserDetails userDetails) {
  
  ...
  
  }
  
  String generateToken(Map<String, Object> claims) {
  
  ...
  
  }
  
  public String refreshToken(String token) {
  
  ...
  
  }
  
  public Boolean validateToken(String token, UserDetails userDetails) {
  
  ...
  
  }
  
  ... // 省略部分工具函数
  
  }
  
  创建Token过滤器,用于每次外部对接口请求时的Token处理
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @Component
  
  public class JwtTokenFilter extends OncePerRequestFilter {
  
  @Autowired
  
  private UserDetailsService userDetailsService;
  
  @Autowired
  
  private JwtTokenUtil jwtTokenUtil;
  
  @Override
  
  protected void doFilterInternal ( HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
  
  String authHeader = request.getHeader( Const.HEADER_STRING );
  
  if (authHeader != null && authHeader.startsWith( Const.TOKEN_PREFIX )) {
  
  final String authToken = authHeader.substring( Const.TOKEN_PREFIX.length() );
  
  String username = jwtTokenUtil.getUsernameFromToken(authToken);
  
  if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
  
  UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
  
  if (jwtTokenUtil.validateToken(authToken, userDetails)) {
  
  UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
  
  userDetails, null, userDetails.getAuthorities());
  
  authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(
  
  request));
  
  SecurityContextHolder.getContext().setAuthentication(authentication);
  
  }
  
  }
  
  }
  
  chain.doFilter(request, response);
  
  }
  
  }
  
  Service业务编写
  
  主要包括用户登录和注册两个主要的业务
  
  public interface AuthService {
  
  User register( User userToAdd );
  
  String login( String username, String password );
  
  }
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @Service
  
  public class AuthServiceImpl implements AuthService {
  
  @Autowired
  
  private AuthenticationManager authenticationManager;
  
  @Autowired
  
  private UserDetailsService userDetailsService;
  
  @Autowired
  
  private JwtTokenUtil jwtTokenUtil;
  
  @Autowired
  
  private UserRepository userRepository;
  
  // 登录
  
  @Override
  
  public String login( String username, String password www.jrgjze.com) {
  
  UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken( username, password );
  
  final Authentication authentication = authenticationManager.authenticate(upToken);
  
  SecurityContextHolder.getContext(www.dfgjpt.com).setAuthentication(authentication);
  
  final UserDetails userDetails = userDetailsService.loadUserByUsername(www.xianjingshuiqi.com username );
  
  final String token = jwtTokenUtil.generateToken(www.yongshi123.cn userDetails);
  
  return token;
  
  }
  
  // 注册
  
  @Override
  
  public User register( User userToAdd www.gen-okamoto.com) {
  
  final String username = userToAdd.getUsername(www.meiwanyule.cn);
  
  if( userRepository.findByUsername(username)www.hengtongyoule.com!=null ) {
  
  return null;
  
  }
  
  BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  
  final String rawPassword = userToAdd.getPassword();
  
  userToAdd.setPassword( encoder.encode(rawPassword) );
  
  return userRepository.save(userToAdd);
  
  }
  
  }
  
  Spring Security配置类编写(非常重要)
  
  这是一个高度综合的配置类,主要是通过重写 WebSecurityConfigurerAdapter 的部分 configure配置,来实现用户自定义的部分。
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @Configuration
  
  @EnableWebSecurity
  
  @EnableGlobalMethodSecurity(www.365soke.com/ prePostEnabled=true)
  
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  
  private UserService userService;
  
  @Bean
  
  public JwtTokenFilter authenticationTokenFilterBean() throws Exception {
  
  return new JwtTokenFilter(www.thd178.com/);
  
  }
  
  @Bean
  
  public AuthenticationManager authenticationManagerBean() throws Exception {
  
  return super.authenticationManagerBean();
  
  }
  
  @Override
  
  protected void configure( AuthenticationManagerBuilder auth ) throws Exception {
  
  auth.userDetailsService( userService ).passwordEncoder( new BCryptPasswordEncoder() );
  
  }
  
  @Override
  
http://dasheng178.com/#portal/list.html

  protected void configure(www.bsylept.com HttpSecurity httpSecurity ) throws Exception {
  
  httpSecurity.csrf(www.ysgptvip.com).disable()
  
  .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  
  .authorizeRequests(www.xinghenyule.com)
  
  .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() // OPTIONS请求全部放行
  
  .antMatchers(HttpMethod.POST, "/authentication/**").permitAll() //登录和注册的接口放行,其他接口全部接受验证
  
  .antMatchers(HttpMethod.POST).authenticated()
  
  .antMatchers(HttpMethod.PUT).authenticated()
  
  .antMatchers(HttpMethod.DELETE).authenticated()
  
  .antMatchers(HttpMethod.GET).authenticated();
  
  // 使用前文自定义的 Token过滤器
  
  httpSecurity
  
  .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
  
  httpSecurity.headers().cacheControl();
  
  }
  
  }
  
  编写测试 Controller
  
  登录和注册的 Controller:
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @RestController
  
  public class JwtAuthController {
  
  @Autowired
  
  private AuthService authService;
  
  // 登录
  
  @RequestMapping(value = "/authentication/login", method = RequestMethod.POST)
  
  public String createToken( String username,String password ) throws AuthenticationException {
  
  return authService.login( username, password ); // 登录成功会返回JWT Token给用户
  
  }
  
  // 注册
  
  @RequestMapping(value = "/authentication/register", method = RequestMethod.POST)
  
  public User register( @RequestBody User addedUser ) throws AuthenticationException {
  
  return authService.register(addedUser);
  
  }
  
  }
  
  再编写一个测试权限的 Controller:
  
  /**
  
  * @ www.codesheep.cn
  
  * 20190312
  
  */
  
  @RestController
  
  public class TestController {
  
  // 测试普通权限
  
  @PreAuthorize("hasAuthority('ROLE_NORMAL')")
  
  @RequestMapping( value="/normal/test", method = RequestMethod.GET )
  
  public String test1() {
  
  return "ROLE_NORMAL /normal/test接口调用成功!";
  
  }
  
  // 测试管理员权限
  
  @PreAuthorize("hasAuthority('ROLE_ADMIN')")
  
  @RequestMapping( value = "/admin/test", method = RequestMethod.GET )
  
  public String test2() {
  
  return "ROLE_ADMIN /admin/test接口调用成功!";
  
  }
  
  }
  
  这里给出两个测试接口用于测试权限相关问题,其中接口 /normal/test需要用户具备普通角色(ROLE_NORMAL)即可访问,而接口/admin/test则需要用户具备管理员角色(ROLE_ADMIN)才可以访问。
  
  接下来启动工程,实验测试看看效果
  
  实验验证
  
  在文章开头我们即在用户表 user中插入了一条用户名为 codesheep的记录,并在用户-角色表 user_roles中给用户 codesheep分配了普通角色(ROLE_NORMAL)和管理员角色(ROLE_ADMIN)
  
  接下来进行用户登录,并获得后台向用户颁发的JWT Token
  
  用户登录并获得JWT Token
  
  接下来访问权限测试接口
  
  不带 Token直接访问需要普通角色(ROLE_NORMAL)的接口 /normal/test会直接提示访问不通:
  
  不带token访问是不通的
  
  而带 Token访问需要普通角色(ROLE_NORMAL)的接口 /normal/test才会调用成功:
  
  带token访问OK
  
  同理由于目前用户具备管理员角色,因此访问需要管理员角色(ROLE_ADMIN)的接口 /admin/test也能成功:
  
  访问需要管理员角色的接口OK
  
  接下里我们从用户-角色表里将用户codesheep的管理员权限删除掉,再访问接口 /admin/test,会发现由于没有权限,访问被拒绝了:
  
  由于权限不够而被拒绝
  
  经过一系列的实验过程,也达到了我们的预期!
  
  写在最后

Spring Security和 JWT两大利器来打造一个简易的权限系统。的更多相关文章

  1. 基于Spring Security和 JWT的权限系统设计

    写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领域,成熟的安全框架解决方案一般有 A ...

  2. 【Spring Cloud & Alibaba 实战 | 总结篇】Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现微服务统一认证授权和鉴权

    一. 前言 hi,大家好~ 好久没更文了,期间主要致力于项目的功能升级和问题修复中,经过一年时间的打磨,[有来]终于迎来v2.0版本,相较于v1.x版本主要完善了OAuth2认证授权.鉴权的逻辑,结合 ...

  3. 用Spring Security, JWT, Vue实现一个前后端分离无状态认证Demo

    简介 完整代码 https://github.com/PuZhiweizuishuai/SpringSecurity-JWT-Vue-Deom 运行展示 后端 主要展示 Spring Security ...

  4. Spring Security整合JWT,实现单点登录,So Easy~!

    前面整理过一篇 SpringBoot Security前后端分离,登录退出等返回json数据,也就是用Spring Security,基于SpringBoot2.1.4 RELEASE前后端分离的情况 ...

  5. Springboot集成Spring Security实现JWT认证

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 Spring Security作为成熟且强大的安全框架,得到许多大厂的青睐.而作为前后端分离的SSO方案,JWT ...

  6. Springboot WebFlux集成Spring Security实现JWT认证

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 在之前的文章<Springboot集成Spring Security实现JWT认证>讲解了如何在传统 ...

  7. 264分析两大利器:264VISA和Elecard StreamEye Tools

    学了264有将近3个月有余,好多时候都在学习老毕的书和反复看JM86的代码,最近才找到264分析两大利器:264VISA和Elecard StreamEye Tools.不由得感叹,恨不逢同时. 简单 ...

  8. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  9. Spring Security + OAuth2 + JWT 基本使用

    Spring Security + OAuth2 + JWT 基本使用 前面学习了 Spring Security 入门,现在搭配 oauth2 + JWT 进行测试. 1.什么是 OAuth2 OA ...

随机推荐

  1. 一头雾水的"Follow The Pointer"

    原文:一头雾水的"Follow The Pointer" 一头雾水的"Follow The Pointer"                           ...

  2. 【php增删改查实例】第二十一节 - 用户修改功能

    19.1 添加用户修改的按钮 打开userManage.html,找到新增按钮的地方: 我们不难发现,编辑按钮就差不多应该在新建用户的右边. 那么,假如我现在是新人,对这个项目本身就不太熟悉,那么我得 ...

  3. 汇编 STD和CLD指令

    一.用纯汇编封装函数strcmpW 1.用repnz scasw计算字串长度 2.用repz cmpsw比较字串内容 3.把比较的结果存放在EAX里边返回 __declspec(naked) int ...

  4. python语言程序设计5

    1, 评估函数eval() 去掉参数最外侧引号并执行余下语句的函数. 比如eval("1"),经过运行可以得到数字 1 去得是单双引号,不是括号哦.. 广泛来说,能将任何字符串的形 ...

  5. Linux mount 命令

    mount 命令用来挂载文件系统.其基本命令格式为:mount -t type [-o options] device dirdevice:指定要挂载的设备,比如磁盘.光驱等.dir:指定把文件系统挂 ...

  6. 解决 webpack-dev-server 不能自动刷新的问题

    原文发表于我的技术博客 此文主要帮助大家解决 webpack-dev-server 启动后修改源文件浏览器不能自动刷新的问题. 原文发表于我的技术博客 1. webpack 不能热加载的问题 主要的问 ...

  7. nginx通过https方式反向代理多实例tomcat

    案例说明:前面一层nginx+Keepalived部署的LB,后端两台web服务器部署了多实例的tomcat,通过https方式部署nginx反向代理tomcat请求.配置一如下: 1)LB层的ngi ...

  8. B. Divisor Subtraction

    链接 [http://codeforces.com/contest/1076/problem/B] 题意 给你一个小于1e10的n,进行下面的运算,n==0 结束,否则n-最小质因子,问你进行多少步 ...

  9. A. Vasya and Chocolate

    链接 [http://codeforces.com/contest/1065/problem/A] 分析 一个公式完事 代码 #include<bits/stdc++.h> using n ...

  10. 1017 B. The Bits

    链接 [http://codeforces.com/contest/1017/problem/B] 题意 给你两个长度为n,包含0和1的字符串a和b,有一种操作swap a中的任意两个字符使得a&am ...