微服务集成Spring Security + Oauth2 + JWT+Swagger2 + Druid

2024-02-11 17:08

本文主要是介绍微服务集成Spring Security + Oauth2 + JWT+Swagger2 + Druid,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考《深入理解Spring Cloud与微服务构建》 感谢作者 方志朋
参考 https://blog.csdn.net/yuanlaijike/category_9283872.html
参考 https://www.jianshu.com/p/19059060036b

文章目录

      • 背景介绍
      • 实现目标
      • 实现过程
          • 集成网关zuul
          • 集成oauth2
          • 集成swagger2
          • 集成Druid
      • 验证功能

背景介绍

本文是下文的进阶篇,进一步以微服务为基础进行集成Spring Security + Oauth2 + JWT+Swagger2 + Druid
微服务自动化部署SpringCloud+Dockerfile+docker-compose+git+Maven

实现目标

把网关服务和验证服务集成到一个微服务中,验证服务以oauth2+jwt进行实现,同时使用swagger2简单明了的展示验证登入的相关接口,druid方便管理数据库连接池以及性能排查

  • 模块authservice
    • 集成网关zuul 取消原ui模块网关
    • 集成Spring Security Oauth2验证+鉴权
      • Oauth server 获取令牌token
      • Oauth client 用户注册API
  • GibHub项目代码 分支master

实现过程

集成网关zuul
//注意 Spring Boot 1.2x的zuul与oauth2集成会报spring注入错误和unable to start embedded tomcat错误
//Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.cloud.security.oauth2.resource.ResourceServerProperties org.springframework.cloud.security.oauth2.proxy.OAuth2ProxyAutoConfiguration.resourceServerProperties; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.security.oauth2.resource.ResourceServerProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
//参考:https://github.com/spring-cloud/spring-cloud-security/issues/73
//解决办法是升级 Spring Cloud 版本为Brixton.SR5,Spring Boot版本为1.3.5
//新增模块 authservice,网关zuul可参考ui模块配置
//包含 config、eureka、feign、ribbon、hystrix、zuul//config模块添加文件 authservice.yml
ribbon:ReadTimeout: 60000ConnectTimeout: 20000zuul:host:connect-timeout-millis: 20000socket-timeout-millis: 60000routes:authservice:path: /uiservice/**# 相当于把http://localhost/uiservice/xxx中的/xxx段映射到原来的http://localhost/ui/xxx serviceId: uisensitiveHeaders:
集成oauth2
//添加依赖oauth2,默认包含security、oauth2、jwt、lombok包
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>//oauth2是实现Spring Security验证模块,所以先直接进行Spring Security的配置
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class GlobalMethodSecurityConfiguration {}@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Override@Beanpublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}@Overrideprotected void configure(HttpSecurity http) throws Exception {//CSRF:因为不再依赖于Cookie,所以你就不需要考虑对CSRF(跨站请求伪造)的防范。http.csrf().disable().exceptionHandling().authenticationEntryPoint(new AuthenticationEntryPoint() {@Overridepublic void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);}}).and().authorizeRequests().antMatchers("/**").authenticated().and().httpBasic();}@AutowiredUserServiceDetail userServiceDetail;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userServiceDetail).passwordEncoder(new BCryptPasswordEncoder());}
}//注意上面注入的userServiceDetail需要自己实现接口UserDetailsService,security jpa实体表可以自动创建,但是oauth的表需要自行创建,语句见源码的sql.sql
@Service
public class UserServiceDetail implements UserDetailsService {@Autowiredprivate UserDao userRepository;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {return userRepository.findByUsername(username);}
}//同时也是资源服务器,配置如下
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter{Logger log = LoggerFactory.getLogger(ResourceServerConfiguration.class);@Overridepublic void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().regexMatchers(".*swagger.*",".*v2.*",".*webjars.*","/user/login.*","/user/registry.*","/user/test.*",".*druid.*").permitAll().antMatchers("/**").authenticated();}@Overridepublic void configure(ResourceServerSecurityConfigurer resources) throws Exception {log.info("Configuring ResourceServerSecurityConfigurer ");resources.resourceId("authservice").tokenStore(tokenStore);}@AutowiredTokenStore tokenStore;
}//oauth2配置,定义内部oauth server的basic验证用户名密码,并加入jwt私钥解密方式
@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("authservice").secret("123456").scopes("service").autoApprove(true).authorizedGrantTypes("implicit","refresh_token", "password", "authorization_code").accessTokenValiditySeconds(24*3600);//24小时过期}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {endpoints.tokenStore(tokenStore()).tokenEnhancer(jwtTokenEnhancer()).authenticationManager(authenticationManager);}@Autowired@Qualifier("authenticationManagerBean")private AuthenticationManager authenticationManager;@Beanpublic TokenStore tokenStore() {return new JwtTokenStore(jwtTokenEnhancer());}@Beanprotected JwtAccessTokenConverter jwtTokenEnhancer() {KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("fzp-jwt.jks"), "fzp123".toCharArray());JwtAccessTokenConverter converter = new JwtAccessTokenConverter();converter.setKeyPair(keyStoreKeyFactory.getKeyPair("fzp-jwt"));return converter;}
}//jwt配置,公钥加密方式
@Configuration
public class JwtConfiguration {@AutowiredJwtAccessTokenConverter jwtAccessTokenConverter;@Bean@Qualifier("tokenStore")public TokenStore tokenStore() {System.out.println("Created JwtTokenStore");return new JwtTokenStore(jwtAccessTokenConverter);}@Beanprotected JwtAccessTokenConverter jwtTokenEnhancer() {JwtAccessTokenConverter converter =  new JwtAccessTokenConverter();Resource resource = new ClassPathResource("public.cert");String publicKey ;try {publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));} catch (IOException e) {throw new RuntimeException(e);}converter.setVerifierKey(publicKey);return converter;}
}
集成swagger2
//加入依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency>//配置swagger2
@Configuration
@EnableSwagger2
public class SwaggerConfig {/*** 全局参数** @return*/private List<Parameter> parameter() {List<Parameter> params = new ArrayList<>();params.add(new ParameterBuilder().name("Authorization").description("Authorization Bearer token").modelRef(new ModelRef("string")).parameterType("header").required(false).build());return params;}@Beanpublic Docket sysApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.xiaofeng.authservice.controller")).paths(PathSelectors.any()).build().globalOperationParameters(parameter());}private ApiInfo apiInfo() {return new ApiInfoBuilder().title(" authservice api ").description("authservice 微服务").termsOfServiceUrl("").contact("xiaofeng").version("1.0").build();}
}//添加controller
@RestController
@RequestMapping("/user")
public class UserController {@AutowiredUserService userService;@ApiOperation(value = "注册", notes = "username和password为必选项")@RequestMapping(value = "/registry",method = RequestMethod.POST)public User createUser(@RequestBody User user){//参数判读省略,判读该用户在数据库是否已经存在省略String entryPassword= BPwdEncoderUtils.BCryptPassword(user.getPassword());user.setPassword(entryPassword);return userService.createUser(user);}@ApiOperation(value = "登录", notes = "username和password为必选项")@RequestMapping(value = "/login",method = RequestMethod.POST)public RespDTO login(@RequestParam String username , @RequestParam String password){//参数判读省略return   userService.login(username,password);}@ApiOperation(value = "根据用户名获取用户", notes = "根据用户名获取用户")@RequestMapping(value = "/{username}",method = RequestMethod.POST)@PreAuthorize("hasRole('USER')")// @PreAuthorize("hasAnyAuthority('ROLE_USER')")public RespDTO getUserInfo(@PathVariable("username") String username){//参数判读省略User user=  userService.getUserInfo(username);return RespDTO.onSuc(user);}
}
集成Druid
//数据库连接池druid
//config模块的文件 authservice.yml添加
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8username: rootpassword: rootinitialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 20filters: stat,wall,log4jconnectionProperties:druid.stat.mergeSql: truedruid.stat.slowSqlMillis: 5000useGlobalDataSourceStat: truejpa:hibernate:ddl-auto: updateshow-sql: true//Druid配置
@Configuration
public class DruidConfiguration {@Beanpublic ServletRegistrationBean DruidStatViewServle2() {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");//白名单:servletRegistrationBean.addInitParameter("allow", "127.0.0.1");//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.//servletRegistrationBean.addInitParameter("deny", "192.168.1.73");//登录查看信息的账号密码.servletRegistrationBean.addInitParameter("loginUsername", "admin2");servletRegistrationBean.addInitParameter("loginPassword", "123456");//是否能够重置数据.servletRegistrationBean.addInitParameter("resetEnable", "false");return servletRegistrationBean;}/*** 注册一个:filterRegistrationBean** @return*/@Beanpublic FilterRegistrationBean druidStatFilter2() {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());//添加过滤规则.filterRegistrationBean.addUrlPatterns("/*");//添加不需要忽略的格式信息.filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}}

验证功能

  • Spring Security + Oauth2 + JWT+Swagger2
    按顺序启动 Discovery、Config、Authservice
    首先打开swagger2,查看现有接口:http://localhost/swagger-ui.html
    使用curl进行请求验证,参考:
    https://blog.csdn.net/suicuney/article/details/103847400
//模拟登入
curl -X POST -d username=admin -d password=admin http://localhost:9999/user/login
//模拟从oauth server 获取token
curl -X POST -u authservice:123456 http://localhost:9999/oauth/token -d grant_type=password -d username=admin -d password=admin
//模拟从oauth server 获取token 另一种写法
curl -X POST -H "Authorization: Basic YXV0aHNlcnZpY2U6MTIzNDU2" -d grant_type=password -d username=admin -d password=admin http://localhost:9999/oauth/token
//模拟用登入得到token进行访问有权限的controll接口
curl -X POST -H "Authorization:BearereyJhbGciOiJSUzI1NiJ9.eyJleHAiOjE1Nzg1Mzc4NDIsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiVVNFUiJdLCJqdGkiOiI4Y2JkYjRjMy0wOTMxLTQyMWItYWYwOS1lMDViYzYyODFhY2YiLCJjbGllbnRfaWQiOiJhdXRoc2VydmljZSIsInNjb3BlIjpbInNlcnZpY2UiXX0.JNdRVRcOau0uTmUdSVdImeQPcIf6PcBzIL3bdfm0856ou9EjEiGDbqike1nw2DueR3Kq5AnbtsYuiPA_sEuwamFLy3H2Eezo7y-5DX26fId7PufHWn1aSshsW5zQNGORr47xZ8_oXq2J5yfwzCrDNDzqbgkcOAB7jWTD9DcOPUig2FCvA0AglZxt442W34N_Sds6l8C6Hy9Dl2hlzAoe0VCy_yCv2APnwNhX4KWnFJTZsEK9LeYgwvlM0nPz6JOYwOlLSk4P8geC0zuspoJ0Ve9mXU4qHzX040amrSjnJooLL1jmsxDVffop6rprkQmuKSkEDfipVfRLx5TUB9xv4g","token_type":"bearer","refresh_token":"eyJhbGciOiJSUzI1NiJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbInNlcnZpY2UiXSwiYXRpIjoiOGNiZGI0YzMtMDkzMS00MjFiLWFmMDktZTA1YmM2MjgxYWNmIiwiZXhwIjoxNTgxMDQzNDQyLCJhdXRob3JpdGllcyI6WyJVU0VSIl0sImp0aSI6IjFjNzc3ZjE4LWU0YjEtNGYzNi05ZTE5LTIwYzZiNDRmZGMyOCIsImNsaWVudF9pZCI6ImF1dGhzZXJ2aWNlIn0.YmcMlCzEmLtN_W6XrlP3O5EkH_jbH6Gpa8rxiuVRpW6k9R3k77ZZauE07f1v_dyUL3DGRzuMMGDGfKHOUapJ9gus2UX9-QDe9x9V46hEVkfcHplYwwdC43o8Z6URM4rlA5vJkKbQa6EI1KJVZfLNkfTSXjE2TD3M2MuwJu4xgkNg6Eg25vDxyiFPIsyOBIl66ROJJogS90M7tMrOiCTK40jWTPwrDfOdy7EzJvi0mlCwZmGK9qP2pwB8yi5zcgHT2P0XrPFT_VPDQJS5X7DLU_k-k_mfoHyobtVHoDF7VkUdLngtoqy_ynF-hcJqvHH8PvnwjaVzi448dGpXXBSaxA" http://localhost:9999/user/admin
  • Druid监控
    打开druid监控网址:http://localhost/druid/login.html
    用户名密码为配置类中设置的admin2/123456
    登入后可以查看数据库各种相关信息
    在这里插入图片描述

这篇关于微服务集成Spring Security + Oauth2 + JWT+Swagger2 + Druid的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/700356

相关文章

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

Java中的密码加密方式

《Java中的密码加密方式》文章介绍了Java中使用MD5算法对密码进行加密的方法,以及如何通过加盐和多重加密来提高密码的安全性,MD5是一种不可逆的哈希算法,适合用于存储密码,因为其输出的摘要长度固... 目录Java的密码加密方式密码加密一般的应用方式是总结Java的密码加密方式密码加密【这里采用的

Java中ArrayList的8种浅拷贝方式示例代码

《Java中ArrayList的8种浅拷贝方式示例代码》:本文主要介绍Java中ArrayList的8种浅拷贝方式的相关资料,讲解了Java中ArrayList的浅拷贝概念,并详细分享了八种实现浅... 目录引言什么是浅拷贝?ArrayList 浅拷贝的重要性方法一:使用构造函数方法二:使用 addAll(

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b