spring security oauth2 login集成码云,定制部分token端点和user_info端点的逻辑

spring security oauth2只需要配置一下provider的元数据就能进行oauth2登陆了,但是我在集成码云的时候遇到了两个问题,一个是restTemple在发送http请求如果不加user-agent请求头会返回403,另一个是在访问user-agent端点的时候security默认把access_token放在头部,但是码云接收的是querystring里面的access_token,所以需要自定义一些对象。

一个是token_uri的bean,另一个是user_info端点的bean。

以下是我的配置:

@EnableWebSecurity
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter{
       @Override
    public void configure(WebSecurity web) {
    	// TODO Auto-generated method stub
    	web.debug(true);
    	
    
    }
       

    @Autowired
    private ClientRegistrationRepository clientRegistrationRepository;
	
	 @Override
	protected void configure(HttpSecurity http) throws Exception {
		// TODO Auto-generated method stub
		 http
		
			.authorizeRequests()
			      
			     .antMatchers("/login*/**")
			     .permitAll()
			     
				.anyRequest().authenticated()
				.and()
				.oauth2Login(
						
						)
										 
				 .tokenEndpoint()
                 .accessTokenResponseClient(this.accessTokenResponseClient())
	             .and()
	             .userInfoEndpoint()
	             .userService(auth2UserService())
	          
			;
	}
	 @Bean
	 public OAuth2AccessTokenResponseClient accessTokenResponseClient(){
		 
		 DefaultAuthorizationCodeTokenResponseClient provider = new DefaultAuthorizationCodeTokenResponseClient();
		    provider.setRequestEntityConverter(new OAuth2AuthorizationCodeGrantRequestEntityConverter() {
		    	@Override
		    	public RequestEntity convert(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) {
		    		
		    		 RequestEntity convert = super.convert(authorizationCodeGrantRequest);
		    		 if(authorizationCodeGrantRequest.getClientRegistration().getRegistrationId().equals("gitee")) {
		    			HttpHeaders headers = new HttpHeaders();
                      //加上user-agent头
		    			headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
		    			headers.addAll(convert.getHeaders());
		    			return new RequestEntity<>(convert.getBody(), headers,HttpMethod.POST,convert.getUrl());
		                       
		    		 }		    		
		    	 return convert;
		    	}
		    });

		 return provider;
		
		 
	 }
	  @Bean
	 public OAuth2UserService auth2UserService() {
		        DefaultOAuth2UserService defaultOAuth2UserService = new DefaultOAuth2UserService();
		        defaultOAuth2UserService.setRequestEntityConverter(new OAuth2UserRequestEntityConverter() {
		        	      @Override
		        	    public RequestEntity convert(OAuth2UserRequest userRequest) {
	
		        	    	 RequestEntity convert = super.convert(userRequest);
				    		 if(userRequest.getClientRegistration().getRegistrationId().equals("gitee")) {
				    			HttpHeaders headers = new HttpHeaders();
				    			headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
				    			headers.addAll(convert.getHeaders());
				    			 
				    		
				  //加上access_token queryString  				
									return new RequestEntity<>(convert.getBody(), headers, HttpMethod.GET, 
URI.create(userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri()+"?access_token="+userRequest.getAccessToken().getTokenValue()));
								
				                       
				    		 }		    		
				    	 return convert;
		        	    }
		        });
		        return defaultOAuth2UserService;
	 }
	 
}

以下是我的springboot依赖


		org.springframework.boot
		spring-boot-starter-parent
		2.2.0.RELEASE
		 
	
	
	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-oauth2-client
		
	
		
		org.springframework.boot
        spring-boot-devtools
          true
        	
		
	
		
			org.springframework.boot
			spring-boot-starter-security
		
		
			org.springframework.boot
			spring-boot-starter-web
		

     
	
		
			org.springframework.security
			spring-security-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

 

你可能感兴趣的:(spring security oauth2 login集成码云,定制部分token端点和user_info端点的逻辑)