FeignClient SpringCloud Access Passport service station in a simple configuration Integration

1, Passport as a unified user account (login registration) center, so the client should be encapsulated into a single SDK for different platforms;

2, all system services visit Passport, must carry appId APP unified identity issued by the Passport;

3, all system services visit Passport, must carry the token after the user logs successes.

 

  • Client Access Passport background service configuration

 

@Configuration
public class CommonConfig implements RequestInterceptor, HandlerInterceptor {

    public static final ThreadLocal<String> Token = new ThreadLocal();

    @Value("${app.id}")
    private String AppID;


    @Bean
    public WebMvcConfigurer webMvcConfigurer(@Autowired CommonConfig config) {
        return new WebMvcConfigurer(){
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
				//	配置需要访问UCenter的请求url
                String[] includePatterns =  {"/**","/passport/**"};
                HandlerInterceptor mappedInterceptor = new MappedInterceptor(includePatterns, null, config);
                registry.addInterceptor (mappedInterceptor); 
            } 
        ;} 
    } 
		// all request header issued by FeignClient will be this configuration, 
		// 2 and 3 in order to satisfy the requirements of all requests issued by the head and added appId token 
        String token = Token.get ();

    @Override
    The preHandle Boolean public (the HttpServletRequest Request, Response the HttpServletResponse, 
                             Object Handler) { 
		App client request header // the token using ThreadLocal cached, 
		when the subsequent request is forwarded to // the Passport, configured token acquisition request header 
        String token = Request. getHeader ( "token"); 
        Token.set (token); 
        return to true; 
    } 

    @Override 
    public void Apply (requestTemplate requestTemplate) { 
        requestTemplate.header ( "for appId", the AppID); 
        requestTemplate.header ( "token", token); 
    } 
}

As shown, spring boot configuration class CommonConfig defined above code, and to achieve RequestInterceptor HandlerInterceptor and interfaces, respectively, to achieve

boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) and void apply (RequestTemplate requestTemplate) side. But also registered WebMvcConfigurer class configuration requests to intercept url, and cache request token header in the intercept method preHandle in.

Subsequently, when the request is forwarded to Passport, AppID join in the request header in the cache token and corresponding service.

Guess you like

Origin www.cnblogs.com/shenjixiaodao/p/12659920.html