⒈JWT?

  JWT(Json Web Token),是Json的一个开放的Token标准。

    1,自包含,SpringSecurityOAuth的默认Token是UUID的一个随机的无意义的字符串,并不包含任何信息,信息是被单独存放的,我们还需要通过这个令牌从单独存放信息的存储那里获取信息,所以说SpringSecurityOAuth的默认Token比较依赖于存储,一旦存储信息的存储那里出现了故障,那么这个令牌就毫无用处了。而JWT Token中是包含有意义的信息的,当我们的系统拿到这个令牌以后,我们可以直接解析这个令牌来获取有用的信息。

    2,密签,我们可以对发出去的令牌进行签名(指令牌被人篡改我们可以获知)

    3,可扩展,Token中的信息我们可以根据业务需求进行扩展。

⒉如何在SpringSecurityOAuth中使用JWT Token替换掉默认的UUID Token

  1.

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-data-redis</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.security.oauth</groupId>
  15. <artifactId>spring-security-oauth2</artifactId>
  16. <version>2.3.5.RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>commons-collections</groupId>
  20. <artifactId>commons-collections</artifactId>
  21. <version>3.2.2</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-test</artifactId>
  26. <scope>test</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.security</groupId>
  30. <artifactId>spring-security-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>

  2.

  1. package cn.coreqi.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Conditional;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.connection.RedisConnectionFactory;
  8. import org.springframework.security.oauth2.provider.token.TokenStore;
  9. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  10. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  11. import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  12.  
  13. @Configuration
  14. public class TokenStoreConfig {
  15.  
  16. @Autowired
  17. private RedisConnectionFactory redisConnectionFactory;
  18.  
  19. /**
  20. * TokenStore 负责令牌的存取
  21. * @return
  22. */
  23. // @Bean
  24. // public TokenStore redisTokenStore(){
  25. // return new RedisTokenStore(redisConnectionFactory);
  26. // }
  27.  
  28. @Configuration
  29. public static class JwtTokenConfig {
  30. /**
  31. * Token生成中的处理
  32. * @return
  33. */
  34. @Bean
  35. public JwtAccessTokenConverter jwtAccessTokenConverter(){
  36. JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
  37. accessTokenConverter.setSigningKey("fanqi"); //Token签名用的密钥
  38. //发出去的令牌需要密钥签名,验令牌的时候也需要令牌来验签,如果他人获知了我们的密钥
  39. //就可以用我们的密钥来签发我们的JWT令牌,JWT唯一的安全性就是密钥
  40. //别人用我们的密钥来签发我们的JWT令牌就可以随意进入我们的系统
  41. return accessTokenConverter;
  42. }
  43.  
  44. @Bean
  45. public TokenStore jwtTokenStore(){
  46. return new JwtTokenStore(jwtAccessTokenConverter());
  47. }
  48. }
  49. }

  3.

  1. package cn.coreqi.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.connection.RedisConnectionFactory;
  8. import org.springframework.security.authentication.AuthenticationManager;
  9. import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  10. import org.springframework.security.core.userdetails.UserDetailsService;
  11. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  12. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  13. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  14. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  15. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  16. import org.springframework.security.oauth2.provider.token.TokenStore;
  17. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  18. import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  19.  
  20. @Configuration
  21. @EnableAuthorizationServer //开启认证服务器
  22. public class CoreqiAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  23.  
  24. @Autowired
  25. //@Qualifier("authenticationManagerBean")
  26. private AuthenticationManager authenticationManager;
  27.  
  28. @Autowired
  29. private UserDetailsService userDetailsService;
  30.  
  31. @Autowired
  32. private TokenStore jwtTokenStore;
  33.  
  34. @Autowired(required = false)
  35. private JwtAccessTokenConverter jwtAccessTokenConverter;
  36.  
  37. // @Autowired
  38. // private AuthenticationConfiguration authenticationConfiguration;
  39.  
  40. /**
  41. * 针对端点的配置
  42. * @param authorizationServerEndpointsConfigurer
  43. * @throws Exception
  44. */
  45. @Override
  46. public void configure(AuthorizationServerEndpointsConfigurer authorizationServerEndpointsConfigurer) throws Exception {
  47. //authorizationServerEndpointsConfigurer.authenticationManager(authenticationConfiguration.getAuthenticationManager());
  48. authorizationServerEndpointsConfigurer.tokenStore(jwtTokenStore) //使用JwtToken
  49. .authenticationManager(authenticationManager)
  50. .userDetailsService(userDetailsService);
  51. if(jwtAccessTokenConverter != null){
  52. authorizationServerEndpointsConfigurer.accessTokenConverter(jwtAccessTokenConverter);
  53. }
  54. }
  55.  
  56. /**
  57. * 第三方应用客户端的有关配置
  58. * @param clientDetailsServiceConfigurer
  59. * @throws Exception
  60. */
  61. @Override
  62. public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
  63. clientDetailsServiceConfigurer.inMemory()
  64. .withClient("coreqi") //client_id
  65. .secret("coreqiSecret") //client_id的密码
  66. .accessTokenValiditySeconds(7200) //令牌的有效时间(单位秒)
  67. .redirectUris("https://www.baidu.com")
  68. .scopes("all","read","write") //所支持的权限有那些
  69. .authorities("COREQI_READ")
  70. .authorizedGrantTypes("authorization_code","password"); //针对当前client所支持的授权模式
  71. }
  72.  
  73. /**
  74. * 针对安全性有关的配置
  75. * @param security
  76. * @throws Exception
  77. */
  78. @Override
  79. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  80. super.configure(security);
  81. }
  82. }

⒊如何扩展Jwt Token中的信息

  1. package cn.coreqi.jwt;
  2.  
  3. import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
  4. import org.springframework.security.oauth2.common.OAuth2AccessToken;
  5. import org.springframework.security.oauth2.provider.OAuth2Authentication;
  6. import org.springframework.security.oauth2.provider.token.TokenEnhancer;
  7.  
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. public class CoreqiJwtTokenEnhancer implements TokenEnhancer {
  12.  
  13. @Override
  14. public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
  15. Map<String,Object> info = new HashMap<>(); //要往accessToken中存放的信息
  16. info.put("company","fanqi");
  17. ((DefaultOAuth2AccessToken)accessToken).setAdditionalInformation(info); //设置附加信息
  18. return accessToken;
  19. }
  20. }
  1. package cn.coreqi.config;
  2.  
  3. import cn.coreqi.jwt.CoreqiJwtTokenEnhancer;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Conditional;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.data.redis.connection.RedisConnectionFactory;
  9. import org.springframework.security.oauth2.provider.token.TokenEnhancer;
  10. import org.springframework.security.oauth2.provider.token.TokenStore;
  11. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  12. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  13. import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  14.  
  15. @Configuration
  16. public class TokenStoreConfig {
  17.  
  18. @Autowired
  19. private RedisConnectionFactory redisConnectionFactory;
  20.  
  21. /**
  22. * TokenStore 负责令牌的存取
  23. * @return
  24. */
  25. // @Bean
  26. // public TokenStore redisTokenStore(){
  27. // return new RedisTokenStore(redisConnectionFactory);
  28. // }
  29.  
  30. @Configuration
  31. public static class JwtTokenConfig {
  32. /**
  33. * Token生成中的处理
  34. * @return
  35. */
  36. @Bean
  37. public JwtAccessTokenConverter jwtAccessTokenConverter(){
  38. JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
  39. accessTokenConverter.setSigningKey("fanqi"); //Token签名用的密钥
  40. //发出去的令牌需要密钥签名,验令牌的时候也需要令牌来验签,如果他人获知了我们的密钥
  41. //就可以用我们的密钥来签发我们的JWT令牌,JWT唯一的安全性就是密钥
  42. //别人用我们的密钥来签发我们的JWT令牌就可以随意进入我们的系统
  43. return accessTokenConverter;
  44. }
  45.  
  46. @Bean
  47. public TokenStore jwtTokenStore(){
  48. return new JwtTokenStore(jwtAccessTokenConverter());
  49. }
  50.  
  51. @Bean
  52. public TokenEnhancer jwtTokenEnhancer(){
  53. return new CoreqiJwtTokenEnhancer();
  54. }
  55. }
  56. }
  1. package cn.coreqi.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.connection.RedisConnectionFactory;
  8. import org.springframework.security.authentication.AuthenticationManager;
  9. import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  10. import org.springframework.security.core.userdetails.UserDetailsService;
  11. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  12. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  13. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  14. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  15. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  16. import org.springframework.security.oauth2.provider.token.TokenEnhancer;
  17. import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
  18. import org.springframework.security.oauth2.provider.token.TokenStore;
  19. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  20. import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  21.  
  22. import java.util.ArrayList;
  23. import java.util.List;
  24.  
  25. @Configuration
  26. @EnableAuthorizationServer //开启认证服务器
  27. public class CoreqiAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  28.  
  29. @Autowired
  30. //@Qualifier("authenticationManagerBean")
  31. private AuthenticationManager authenticationManager;
  32.  
  33. @Autowired
  34. private UserDetailsService userDetailsService;
  35.  
  36. @Autowired
  37. private TokenStore jwtTokenStore;
  38.  
  39. @Autowired(required = false)
  40. private JwtAccessTokenConverter jwtAccessTokenConverter;
  41.  
  42. @Autowired(required = false)
  43. private TokenEnhancer jwtTokenEnhancer;
  44.  
  45. // @Autowired
  46. // private AuthenticationConfiguration authenticationConfiguration;
  47.  
  48. /**
  49. * 针对端点的配置
  50. * @param authorizationServerEndpointsConfigurer
  51. * @throws Exception
  52. */
  53. @Override
  54. public void configure(AuthorizationServerEndpointsConfigurer authorizationServerEndpointsConfigurer) throws Exception {
  55. //authorizationServerEndpointsConfigurer.authenticationManager(authenticationConfiguration.getAuthenticationManager());
  56. authorizationServerEndpointsConfigurer.tokenStore(jwtTokenStore) //使用JwtToken
  57. .authenticationManager(authenticationManager)
  58. .userDetailsService(userDetailsService);
  59. if(jwtAccessTokenConverter != null && jwtTokenEnhancer != null){
  60. TokenEnhancerChain enhancerChain = new TokenEnhancerChain(); //Token增强器
  61. List<TokenEnhancer> enhancers = new ArrayList<>();
  62. enhancers.add(jwtTokenEnhancer);
  63. enhancers.add(jwtAccessTokenConverter);
  64. enhancerChain.setTokenEnhancers(enhancers);
  65. authorizationServerEndpointsConfigurer.tokenEnhancer(enhancerChain)
  66. .accessTokenConverter(jwtAccessTokenConverter);
  67. }
  68. }
  69.  
  70. /**
  71. * 第三方应用客户端的有关配置
  72. * @param clientDetailsServiceConfigurer
  73. * @throws Exception
  74. */
  75. @Override
  76. public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
  77. clientDetailsServiceConfigurer.inMemory()
  78. .withClient("coreqi") //client_id
  79. .secret("coreqiSecret") //client_id的密码
  80. .accessTokenValiditySeconds(7200) //令牌的有效时间(单位秒)
  81. .redirectUris("https://www.baidu.com")
  82. .scopes("all","read","write") //所支持的权限有那些
  83. .authorities("COREQI_READ")
  84. .authorizedGrantTypes("authorization_code","password"); //针对当前client所支持的授权模式
  85. }
  86.  
  87. /**
  88. * 针对安全性有关的配置
  89. * @param security
  90. * @throws Exception
  91. */
  92. @Override
  93. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  94. super.configure(security);
  95. }
  96. }

⒋控制器如何识别Token中扩展的信息

  1. <dependency>
  2. <groupId>io.jsonwebtoken</groupId>
  3. <artifactId>jjwt</artifactId>
  4. <version>0.9.1</version>
  5. </dependency>
  1. package cn.coreqi.controller;
  2.  
  3. import io.jsonwebtoken.Claims;
  4. import io.jsonwebtoken.Jwts;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. import javax.servlet.http.HttpServletRequest;
  10. import java.io.UnsupportedEncodingException;
  11. import java.net.Authenticator;
  12.  
  13. @RestController
  14. public class UserController {
  15.  
  16. @GetMapping("/user/me")
  17. public Object getCurrentUser(Authenticator user , HttpServletRequest request) throws UnsupportedEncodingException {
  18. String header = request.getHeader("Authorization");
  19. String token = StringUtils.substringAfter(header,"bearer ");
  20. Claims claims = Jwts.parser().setSigningKey("fanqi".getBytes("UTF-8")).parseClaimsJws(token).getBody();
  21. String company = (String) claims.get("company");
  22. System.out.println(company);
  23. return user;
  24. }
  25. }

SpringSecurityOAuth使用JWT Token的更多相关文章

  1. SpringSecurityOAuth使用JWT Token实现SSO单点登录

    ⒈认证服务器 1.添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...

  2. Spring Cloud OAuth2.0 微服务中配置 Jwt Token 签名/验证

    关于 Jwt Token 的签名与安全性前面已经做了几篇介绍,在 IdentityServer4 中定义了 Jwt Token 与 Reference Token 两种验证方式(https://www ...

  3. ASP.NET Core 实战:基于 Jwt Token 的权限控制全揭露

    一.前言 在涉及到后端项目的开发中,如何实现对于用户权限的管控是需要我们首先考虑的,在实际开发过程中,我们可能会运用一些已经成熟的解决方案帮助我们实现这一功能,而在 Grapefruit.VuCore ...

  4. 关于 IdentityServer4 中的 Jwt Token 与 Reference Token

    OpenID Connect(Core),OAuth 2.0(RFC 6749),JSON Web Token (JWT)(RFC 7519) 之间有着密不可分联系,对比了不同语言的实现,还是觉得 I ...

  5. 如何在启用JWT Token授权的.NET Core WebApi项目中下载文件

    背景 前几天,做项目的时候遇到一个文件下载的问题.当前系统是一个前后端分离的项目,前端是一个AngularJs项目, 后端是一个.NET Core WebApi项目.后端的Api项目使用了Jwt To ...

  6. 【ASP.NET Core快速入门】(十一)应用Jwtbearer Authentication、生成jwt token

    准备工作 用VSCode新建webapi项目JwtAuthSample,并打开所在文件夹项目 dotnet new webapi --name JwtAuthSample 编辑JwtAuthSampl ...

  7. Django JWT Token RestfulAPI用户认证

    一般情况下我们Django默认的用户系统是满足不了我们的需求的,那么我们会对他做一定的扩展 创建用户项目 python manage.py startapp users 添加项目apps INSTAL ...

  8. 咏南中间件支持JWT TOKEN

    咏南中间件支持JWT TOKEN

  9. 菜鸟入门【ASP.NET Core】11:应用Jwtbearer Authentication、生成jwt token

    准备工作 用VSCode新建webapi项目JwtAuthSample,并打开所在文件夹项目 dotnet new webapi --name JwtAuthSample 编辑JwtAuthSampl ...

随机推荐

  1. webpack打包进内联html

    用到一个新插件:html-webpack-inline-source-plugin(依赖于html-webpack-plugin) 1.安装 npm i -D html-webpack-inline- ...

  2. Spring_xml和注解混合方式开发

    1. spring核心配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  3. ElasticSearch的插件(Plugins)介绍

    ElasticSearch的插件(Plugins)介绍 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 目前可以扩展ElasticSearch功能的插件有很多,比如:添加自定义的映 ...

  4. java中用jdom创建xml文档/将数据写入XML中

    import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...

  5. HDU 1027(数字排列 STL)

    题意是求 n 个数在全排列中的第 m 个序列. 直接用 stl 中的 next_permutation(a, a+n) (这个函数是求一段序列的下一个序列的) 代码如下: #include <b ...

  6. Python复习笔记(五)面向对象

    1. __init__方法 # 1. 为对象在内存 中分配空间 -- 创建对象 # 2. 为对象属性 设置初始值 -- 初始化方法(init)+-------------- # 3. __init__ ...

  7. freemarker迭代list、map等常规操作,将数据放到模板中

    转自:https://blog.csdn.net/wickedvalley/article/details/65937189 一.controller开始准备模型.数据1.po类 package co ...

  8. IDEA导入JUnit4

    Step 1. IDEA最上面一栏的菜单栏中,选File->Project Structure(从上往下第11个),弹出窗口左边有一个列表,选Module. Step 2. 右侧有一个带3个标签 ...

  9. 使用 Quartz.NET 实现作业串行执行

    Ø  前言 前两天,在公司的一个项目中编写 Windows 服务时,需求是当A服务运行完后,B服务才能运行,B服务运行后,C服务才能运行.因为B服务的数据依赖于A服务生成的数据,而C服务的数据又依赖于 ...

  10. JS创建对象之构造函数模式

    function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.sayName = f ...