• 登录实现【Security(登录)+Oauth2(颁发授权码/Token)+JWT(加密Token)】


    认证中心:【Security(登录)+Oauth2(颁发授权码/Token)+JWT(加密Token)】

    资源服务器:Oauth2+JWT(解密Token)校验Token+security

    微服务授权

    1.Sping Cloud Oauth2

    2.授权服务器集成Oauth2

    2.1.导入依赖

    spring-cloud-starter-oauth2 整合了Security ,Oauth2, JWT

    1. org.springframework.cloud
    2. spring-cloud-starter-oauth2

    2.2.改造Security配置

    【注意】这里需要在Security配置类中配置一个AuthenticationManager,因为Oauth2的password模式会使用到

    1. //配置认证管理器,授权模式为“poassword”时会用到
    2. @Bean
    3. public AuthenticationManager authenticationManager() throws Exception {
    4. return super.authenticationManager();
    5. }

    2.3.Oauth2授权服务配置

    1. package cn.wmx.mooccc.config;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.http.HttpMethod;
    6. import org.springframework.security.authentication.AuthenticationManager;
    7. import org.springframework.security.crypto.password.PasswordEncoder;
    8. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
    9. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
    10. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
    11. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
    12. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
    13. import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
    14. import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
    15. import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
    16. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
    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.JwtTokenStore;
    21. import javax.sql.DataSource;
    22. import java.util.Arrays;
    23. /**
    24. * 授权服务配置
    25. * */
    26. @Configuration
    27. //开启授权服务配置
    28. @EnableAuthorizationServer
    29. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    30. //1.客户端详情配置(请求参数)
    31. @Autowired
    32. private DataSource dataSource ;
    33. @Autowired
    34. private PasswordEncoder passwordEncoder ;
    35. //1.1.注册客户端详情Bean,基于数据库,自动操作表:oauth_client_details
    36. @Bean
    37. public JdbcClientDetailsService jdbcClientDetailsService(){
    38. JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
    39. //数据库的秘钥使用了PasswordEncoder加密
    40. jdbcClientDetailsService.setPasswordEncoder(passwordEncoder);
    41. return jdbcClientDetailsService;
    42. }
    43. @Override
    44. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    45. clients.withClientDetails(jdbcClientDetailsService());
    46. }
    47. //2.授权服务端点配置(授权码,令牌)
    48. @Autowired
    49. private AuthenticationManager authenticationManager ;
    50. //2.1.定义授权码服务,连接数据库 oauth_code
    51. @Bean
    52. public JdbcAuthorizationCodeServices jdbcAuthorizationCodeServices(){
    53. return new JdbcAuthorizationCodeServices(dataSource);
    54. }
    55. //2.2.令牌服务配置
    56. //令牌的管理服务
    57. @Bean
    58. public AuthorizationServerTokenServices tokenService(){
    59. //创建默认的令牌服务
    60. DefaultTokenServices services = new DefaultTokenServices();
    61. //指定客户端详情配置
    62. services.setClientDetailsService(jdbcClientDetailsService());
    63. //支持产生刷新token
    64. services.setSupportRefreshToken(true);
    65. //token存储方式
    66. services.setTokenStore(tokenStore());
    67. //设置token增强 - 设置token转换器
    68. TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
    69. tokenEnhancerChain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter()));
    70. services.setTokenEnhancer(tokenEnhancerChain); //jwtAccessTokenConverter()
    71. return services;
    72. }
    73. //2.3.配置Token的存储方案
    74. //基于内存的Token存储
    75. @Bean
    76. public TokenStore tokenStore(){
    77. //return new InMemoryTokenStore();
    78. return new JwtTokenStore(jwtAccessTokenConverter());
    79. }
    80. //2.4.配置令牌转换器 ,设置JWT签名密钥。它可以是简单的MAC密钥,也可以是RSA密钥
    81. private final String sign_key = "123";
    82. //JWT令牌校验工具
    83. @Bean
    84. public JwtAccessTokenConverter jwtAccessTokenConverter(){
    85. JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
    86. //设置JWT签名密钥。它可以是简单的MAC密钥,也可以是RSA密钥
    87. jwtAccessTokenConverter.setSigningKey(sign_key);
    88. return jwtAccessTokenConverter;
    89. }
    90. @Override
    91. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    92. endpoints
    93. //1.密码授权模式需要 密码模式,需要认证管理器
    94. .authenticationManager(authenticationManager)
    95. //2.授权码模式服务
    96. .authorizationCodeServices(jdbcAuthorizationCodeServices())
    97. //3.配置令牌管理服务
    98. .tokenServices(tokenService())
    99. //允许post方式请求
    100. .allowedTokenEndpointRequestMethods(HttpMethod.POST);
    101. }
    102. //2.授权服务安全配置(URL放行)
    103. @Override
    104. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    105. security
    106. //对应/oauth/check_token ,路径公开
    107. .checkTokenAccess("permitAll()")
    108. //允许客户端进行表单身份验证,使用表单认证申请令牌
    109. .allowFormAuthenticationForClients();
    110. }
    111. }

    作用分别如下:

    • ClientDetailsServiceConfigurer :用来配置客户端详情服务:如配置客户端id(client_id)资源id、客户端密钥(secrect)、授权方式、scope等,可以基于内存或jdbc。(可以理解为是对浏览器向授权服务器获取授权码或令牌时需要提交的参数配置)

    • AuthorizationServerEndpointsConfigurer:配置令牌的访问端点url和令牌服务,如配置如何管理授权码(内存或jdbc),如何管理令牌(存储方式,有效时间等等)

    • AuthorizationServerSecurityConfigurer: 用来配置令牌端点的安全约束,如配置对获取授权码,检查token等某些路径进行放行

    2.4.测试

    1.授权码模式测试

    浏览器,使用账号密码登录 【数据库中的数据】

     登录成功后访问

    访问如下地址获取授权码 , 注意修改端口和其他参数

    http://localhost:10040/oauth/authorize?client_id=admin&response_type=code&redirect_uri=http://www.baidu.com

    注意这里的client_id对应的值是数据库中的值

     

    点击approve 批准【这个步骤就是为了获取授权码code】

    再使用Postmain获取Token , 注意修改端口和其他参数 【这个步骤就是为了获取access_token,里面包含了用户的信息】

    1. Url: http://localhost:10040/oauth/token?
    2. client_id=admin&client_secret=123&grant_type=authorization_code&code=3mlI1z&redirect_uri=http://www.baidu.com

    注意client_id和clien_secret都是数据库对应的数据

     记得修改里面的code【code就是前面生成的code】

    然后获取到access_token,里面包含了用户的信息

    2.校验token【获取token中包含的信息】

    访问如下地址校验Token , token值改成自己的Token

    http://localhost:10040/oauth/check_token?token=【上面获取的access_token】

  • 相关阅读:
    Android笔记--卸载
    【ESP32之旅】ESP32-S2 MicroPython环境搭建
    Eureka注册中心
    Java.lang.Character类中isLowerCase()方法具有什么功能呢?
    前端模块化
    科研小白的成长之路——博一年度总结
    C++动态输入一个Vector<int>或Vector<string>当作输入接口
    微信小程序商城迅速流行的决定因素
    springboot闲置衣物捐赠系统毕业设计源码021009
    计算机毕业设计(附源码)python自习室管理系统
  • 原文地址:https://blog.csdn.net/m0_64210833/article/details/126096487