当前位置:   article > 正文

青锋开源架构-springboot2.6.x+vue3-antdesign-vite之springboot整合springsecurity_vue3 springboot+springsecurity

vue3 springboot+springsecurity

框架开源地址:

https://gitee.com/msxy/qingfeng-springboot-vue3-antdesign-viteicon-default.png?t=M276https://gitee.com/msxy/qingfeng-springboot-vue3-antdesign-vite

1、加入springsecurity依赖

  1. <!--security依赖-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. </dependency>
  6. <!--oauth2依赖-->
  7. <dependency>
  8. <groupId>org.springframework.security.oauth</groupId>
  9. <artifactId>spring-security-oauth2</artifactId>
  10. <version>2.3.4.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.security</groupId>
  14. <artifactId>spring-security-jwt</artifactId>
  15. <version>1.1.1.RELEASE</version>
  16. </dependency>

2、创建认证服务类

认证服务器需要创建三大配置

认证令牌存储redis

在认证服务中,tokenStore使用的是RedisTokenStore,认证服务器生成的令牌将被存储到Redis中。此处我们需要引入redis用于存储认证服务的令牌token。

  • 认证服务配置:负责发放、校验令牌是否正确
  • 资源服务配置:这一项是可选的,因为认证服务器同样也可以是一个资源服务器。
  • WebSecurity配置:Security配置,主要处理除资源服务外的其他服务请求以及验证token的授权信息
    我们先创建认证服务类AuthorizationServerConfigure:
    1. package com.qingfeng.auth.configure;
    2. import com.qingfeng.auth.service.UserDetailServiceImpl;
    3. import com.qingfeng.auth.translator.MyWebResponseExceptionTranslator;
    4. import com.qingfeng.framework.properties.AuthProperties;
    5. import com.qingfeng.framework.properties.ClientsProperties;
    6. import org.apache.commons.lang3.ArrayUtils;
    7. import org.apache.commons.lang3.StringUtils;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.context.annotation.Bean;
    10. import org.springframework.context.annotation.Configuration;
    11. import org.springframework.data.redis.connection.RedisConnectionFactory;
    12. import org.springframework.security.authentication.AuthenticationManager;
    13. import org.springframework.security.crypto.password.PasswordEncoder;
    14. import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder;
    15. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
    16. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
    17. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
    18. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
    19. import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
    20. import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;
    21. import org.springframework.security.oauth2.provider.token.TokenStore;
    22. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
    23. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
    24. /**
    25. * @title: AuthorizationServerConfigure
    26. * @projectName: AuthorizationServerConfigure
    27. * @description: TODO
    28. * @author: Administrator
    29. * @date: 2021/2/21 0021 21:51
    30. */
    31. @Configuration
    32. @EnableAuthorizationServer
    33. public class AuthorizationServerConfigure extends AuthorizationServerConfigurerAdapter {
    34. @Autowired
    35. private AuthenticationManager authenticationManager;
    36. @Autowired
    37. private RedisConnectionFactory redisConnectionFactory;
    38. @Autowired
    39. private UserDetailServiceImpl userDetailService;
    40. @Autowired
    41. private PasswordEncoder passwordEncoder;
    42. @Autowired
    43. private AuthProperties authProperties;
    44. @Autowired
    45. private MyWebResponseExceptionTranslator exceptionTranslator;
    46. // @Override
    47. // public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    48. // clients.inMemory()
    49. // .withClient("qingfeng")
    50. // .secret(passwordEncoder.encode("123456"))
    51. // .authorizedGrantTypes("password", "refresh_token")
    52. // .scopes("all");
    53. // }
    54. @Override
    55. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    56. ClientsProperties[] clientsArray = authProperties.getClients();
    57. InMemoryClientDetailsServiceBuilder builder = clients.inMemory();
    58. if (ArrayUtils.isNotEmpty(clientsArray)) {
    59. for (ClientsProperties client : clientsArray) {
    60. if (StringUtils.isBlank(client.getClient())) {
    61. throw new Exception("client不能为空");
    62. }
    63. if (StringUtils.isBlank(client.getSecret())) {
    64. throw new Exception("secret不能为空");
    65. }
    66. String[] grantTypes = StringUtils.splitByWholeSeparatorPreserveAllTokens(client.getGrantType(), ",");
    67. builder.withClient(client.getClient())
    68. .secret(passwordEncoder.encode(client.getSecret()))
    69. .authorizedGrantTypes(grantTypes)
    70. .scopes(client.getScope())
    71. .accessTokenValiditySeconds(authProperties.getAccessTokenValiditySeconds())
    72. .refreshTokenValiditySeconds(authProperties.getRefreshTokenValiditySeconds());
    73. }
    74. }
    75. }
    76. @Override
    77. public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    78. endpoints.tokenStore(tokenStore())
    79. .accessTokenConverter(jwtAccessTokenConverter())
    80. .userDetailsService(userDetailService)
    81. .authenticationManager(authenticationManager)
    82. // .tokenServices(defaultTokenServices())
    83. .exceptionTranslator(exceptionTranslator);
    84. }
    85. @Bean
    86. public TokenStore tokenStore() {
    87. // return new RedisTokenStore(redisConnectionFactory);
    88. return new JwtTokenStore(jwtAccessTokenConverter());
    89. }
    90. // @Primary
    91. // @Bean
    92. // public DefaultTokenServices defaultTokenServices() {
    93. // DefaultTokenServices tokenServices = new DefaultTokenServices();
    94. // tokenServices.setTokenStore(tokenStore());
    95. // tokenServices.setSupportRefreshToken(true);
    96. tokenServices.setAccessTokenValiditySeconds(60 * 60 * 24);
    97. tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 7);
    98. // tokenServices.setAccessTokenValiditySeconds(authProperties.getAccessTokenValiditySeconds());
    99. // tokenServices.setRefreshTokenValiditySeconds(authProperties.getRefreshTokenValiditySeconds());
    100. // return tokenServices;
    101. // }
    102. @Bean
    103. public JwtAccessTokenConverter jwtAccessTokenConverter() {
    104. JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
    105. DefaultAccessTokenConverter defaultAccessTokenConverter = (DefaultAccessTokenConverter) accessTokenConverter.getAccessTokenConverter();
    106. DefaultUserAuthenticationConverter userAuthenticationConverter = new DefaultUserAuthenticationConverter();
    107. userAuthenticationConverter.setUserDetailsService(userDetailService);
    108. defaultAccessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
    109. accessTokenConverter.setSigningKey("qingfeng");
    110. return accessTokenConverter;
    111. }
    112. }

    我们来介绍下认证服务类,首先@EnableAuthorizationServer表示开始认证服务。而且继承了AuthorizationServerConfigurerAdapter适配器,AuthorizationServerConfigurerAdapter包含了3个方法:

    1. public class AuthorizationServerConfigurerAdapter implements AuthorizationServerConfigurer {
    2. public AuthorizationServerConfigurerAdapter() {
    3. }
    4. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    5. }
    6. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    7. }
    8. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    9. }
    10. }

    分别用于配置令牌端点的安全约束、客户端信息、配置令牌端点的安全约束

    这也是我们AuthorizationServerConfigure类的骨架,其他代码都是围绕这3个方法产生的,都有注解说明。
    接下来我们分别介绍下以下几个Bean:

  • passwordEncoder:定义加密算法
  • tokenStore:定义token存储方式
  • clientDetailsService:定义客户端client信息
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号