一 网关作用
1.1网关的使用方式
网关整合 OAuth2.0 有 两种思路:
1.网关实现过滤拦截: 一种是认证服务器生成jwt令牌, 所有请求统一在网关层验证,判断权限等操作;
2.各个微服务自己进拦截解析过滤:另一 种是由各资源服务处理,网关只做请求转发 。
1.2 本案例的方式
本案例就是API 网关作为 OAuth2.0 的资源服务器角色,实现接入客户端权限拦截、令牌解析并转发当 前 登录用户信息(jsonToken) 给微服务,这样下游微服务就不需要关心令牌格式解析以 OAuth2.0 相关机制了。
即本案例网关的作用:
1.API 网关在认证授权体系里主要负责两件事:
( 1 ) 作为OAuth2.0的资源服务器角色,实现接入方权限拦截。
( 2 ) 令牌解析并转发当前登录用户信息(明文token)给微服务,
2.微服务拿到明文token( 明文 token 中包含登录用户的身份和权限信息 ) 后也需要做两件事:
( 1 ) 用户授权拦截(看当前用户是否有权访问该资源)
( 2 ) 将用户信息存储进当前线程上下文(有利于后续业务逻辑随时获取当前用户信息)
1.3 zuul前置过滤器使用
参考下文3.3章节
二 . 工程搭建
2.0 本案例网关的作用
1.API 网关在认证授权体系里主要负责两件事:
( 1 ) 作为OAuth2.0的资源服务器角色,实现接入方权限拦截。
( 2 ) 令牌解析并转发当前登录用户信息(明文token)给微服务,
2.微服务拿到明文token( 明文 token 中包含登录用户的身份和权限信息 ) 后也需要做两件事:
( 1 ) 用户授权拦截(看当前用户是否有权访问该资源)
( 2 ) 将用户信息存储进当前线程上下文(有利于后续业务逻辑随时获取当前用户信息)
2.1 工程结构
2.2 pom文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spt-ds-oauth-server</artifactId>
<groupId>com.ljf.springsecurity.oauth</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ljf.springsecurity.oauth</groupId>
<artifactId>spt-ds-gateway-api</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>
<dependency>
<groupId>javax.interceptor</groupId>
<artifactId>javax.interceptor-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2.3 application配置文件
spring.application.name=gateway-server
server.port=53010
spring.main.allow-bean-definition-overriding = true
logging.level.root = info
logging.level.org.springframework = info
zuul.retryable = true
zuul.ignoredServices = *
zuul.add-host-header = true
zuul.sensitiveHeaders = *
zuul.routes.uaa-service.stripPrefix = false
zuul.routes.uaa-service.path = /uaa/**
zuul.routes.order-service.stripPrefix = false
zuul.routes.order-service.path = /order/**
eureka.client.serviceUrl.defaultZone = http://localhost:53000/eureka/
eureka.instance.preferIpAddress = true
eureka.instance.instance-id = ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
management.endpoints.web.exposure.include = refresh,health,info,env
feign.hystrix.enabled = true
feign.compression.request.enabled = true
feign.compression.request.mime-types[0] = text/xml
feign.compression.request.mime-types[1] = application/xml
feign.compression.request.mime-types[2] = application/json
feign.compression.request.min-request-size = 2048
feign.compression.response.enabled = true
统一认证服务( UAA )与统一用户服务都是网关下微服务,需要在网关上新增路由配置:
图中标红的部分需要根据实际情况进行修改
请求url若符合/order/**表达式,将被被转发至order-service(资源服务)
。
请求url若符合/uaa/**表达式,将被被转发至uaa-service(授权服务)。
2.4 启动类
package com.ljf.springsecurity.oauth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GateWayApp
{
public static void main( String[] args )
{
SpringApplication.run(GateWayApp.class, args);
}
}
2.5 启动服务
三 . gateway网关的配置
3.1 token配置文件
资源服务器 由 于需要验证并解析令牌 ,往往可以通过在授权服务器暴露 check_token 的 Endpoint 来
完成,而我们在 授权服务器使用的是对称加密的jwt ,因此知道密钥即可 ,资源服务与授权服务本就是对称设计
1.将order工程中的tokenConfig文件复制到此工程下。如下图所示:
2.代码
package com.ljf.springsecurity.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
/**
* @ClassName: TokenConfig
* @Description: TODO
* @Author: liujianfu
* @Date: 2021/08/29 12:49:39
* @Version: V1.0
**/
@Configuration
public class TokenConfig {
private String SIGNING_KEY = "uaa123";
@Bean
public TokenStore tokenStore() {
//JWT令牌存储方案
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(SIGNING_KEY); //对称秘钥,资源服务器使用该秘钥来验证
return converter;
}
/**
@Bean
public TokenStore tokenStore() {
//使用内存存储令牌(普通令牌)
return new InMemoryTokenStore();
}
**/
}
配置资源服务
在 ResouceServerConfig 中定义资源服务配置,主要配置的内容就是定义一些匹配规则,描述某个接入客户端需要 什么样的权限才能访问某个微服务,如:定义了两个微服务的资源。
3.2.1 UAA服务配置
在ResouceServerConfig中,新增一个UAAServerConfig内部类。UAAServerConfig 中若请求匹配/uaa/**,则网关不进行拦截。
3.2.2 OrderServer服务配置
在ResouceServerConfig中,新增一个OrderServerConfig内部类,OrderServerConfig中,若请求匹配/order/**,客户端scope必须由此参数,且此参数的值必须包含ROLE_API。
由于 res1 这个接入客户端, read 包括 ROLE_ADMIN,ROLE_USER,ROLE_API 三个权限,则会进行拦截处理判断。
将order服务工程中的: ResouceServerConfig配置文件复制过来。
3.2.3 完整代码
package com.ljf.springsecurity.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
/**
* @author Administrator
* @version 1.0
**/
@Configuration
public class ResouceServerConfig {
public static final String RESOURCE_ID = "res1";
//uaa资源服务配置
@Configuration
@EnableResourceServer
public class UAAServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Override
public void configure(ResourceServerSecurityConfigurer resources){
resources.tokenStore(tokenStore).resourceId(RESOURCE_ID)
.stateless(true);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/uaa/**").permitAll();
}
}
//order资源
//uaa资源服务配置
@Configuration
@EnableResourceServer
public class OrderServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Override
public void configure(ResourceServerSecurityConfigurer resources){
resources.tokenStore(tokenStore).resourceId(RESOURCE_ID)
.stateless(true);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/order/**").access("#oauth2.hasScope('ROLE_API')");
}
}
//配置其它的资源服务..
}
安全配置
将WebSecurityConfig安全配置文件复制过来:
转发明文token给微服务*
通过Zuul过滤器的方式实现,目的是让下游微服务能够很方便的获取到当前的登录用户信息(明文token)
3.3.1 实现Zuul前置过滤器
实现Zuul前置过滤器,完成当前登录用户信息提取,并放入转发微服务的request中
3.3.2 完整代码
package com.ljf.springsecurity.filter;
import com.alibaba.fastjson.JSON;
import com.ljf.springsecurity.common.EncryptUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Administrator
* @version 1.0
**/
public class AuthFilter extends ZuulFilter {
@Override
public boolean shouldFilter() {
return true;
}
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
//1.从安全上下文中拿 到用户身份对象
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(!(authentication instanceof OAuth2Authentication)){
return null;
}
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
//2.取出用户身份信息
String principal = userAuthentication.getName();
//3.取出用户权限
List<String> authorities = new ArrayList<>();
//从userAuthentication取出权限,放在authorities
userAuthentication.getAuthorities().stream().forEach(c->authorities.add(((GrantedAuthority) c).getAuthority()));
OAuth2Request oAuth2Request = oAuth2Authentication.getOAuth2Request();
Map<String, String> requestParameters = oAuth2Request.getRequestParameters();
Map<String,Object> jsonToken = new HashMap<>(requestParameters);
if(userAuthentication!=null){
jsonToken.put("principal",principal);
jsonToken.put("authorities",authorities);
}
System.out.println("发送token:"+JSON.toJSONString(jsonToken));
//4.把身份信息和权限信息放在json中,加入http的header中,转发给微服务
ctx.addZuulRequestHeader("json-token", EncryptUtil.encodeUTF8StringBase64(JSON.toJSONString(jsonToken)));
return null;
}
}
3.3.3 将fifilter纳入spring 容器
3.3.4 完整代码
package com.ljf.springsecurity.config;
import com.ljf.springsecurity.filter.AuthFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* @author Administrator
* @version 1.0
**/
@Configuration
public class ZuulConfig {
@Bean
public AuthFilter preFileter() {
return new AuthFilter();
}
@Bean
public FilterRegistrationBean corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
config.setMaxAge(18000L);
source.registerCorsConfiguration("/**", config);
CorsFilter corsFilter = new CorsFilter(source);
FilterRegistrationBean bean = new FilterRegistrationBean(corsFilter);
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}