Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TokenEnhancer不仅向jwt而且向Response对象添加了额外的信息_Java_Spring_Spring Boot_Oauth 2.0_Jwt - Fatal编程技术网

Java TokenEnhancer不仅向jwt而且向Response对象添加了额外的信息

Java TokenEnhancer不仅向jwt而且向Response对象添加了额外的信息,java,spring,spring-boot,oauth-2.0,jwt,Java,Spring,Spring Boot,Oauth 2.0,Jwt,请帮助从响应对象中排除其他信息,并仅添加到JWT 我添加了tokenEnhancer来向JWT添加额外的信息,但它也出现在response对象中 { “访问令牌”:"2.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6六六六六六六

请帮助从响应对象中排除其他信息,并仅添加到
JWT

我添加了
tokenEnhancer
来向
JWT
添加额外的信息,但它也出现在response对象中

{ “访问令牌”:"2.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6.6六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六六Q在中国,这一数字是一个非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非营利的非非营利的非非非SOLP9-fRgbHu_teqOQe5v_gK3f52xr4jBYs2w“, “令牌类型”:“承载者”, “刷新令牌”:Eyjhbgcioizuzi1NIISINR5cIkPxVCJ9.Eyjhdwqiolsib2f1ggylxjlc291cmnl0simz1bgxfBmFtzsi6imhlewjhdcBnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxV2iWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxWnxV5IxWnxWnxWnzId2.在中国,这是一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的中国的一个中国的一个中国的一个中国的一个中国的中国的政府的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的中国的政府的中国的政府的政府的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的一个中国的中国的中国的政府的政府的政府的政府的政府的一个中国的一个中国的一个中国的一个中国的政府的政府的DQMx0KL53CWZrIzoidhLcJ6jJk2Cm7oMI5qobcHx7sGD5OC49UYn9LSHQzqh6RqRhckI5g“, “到期日”:179, “范围”:“读写信任”, “全名”:“heybat guliyev”, “jti”:“cf9ff4d3-8fbb-45ae-ba31-ac42534ac426”}

正如您所看到的,响应对象中也会显示全名

package com.hqsoft.shop.config;

import com.hqsoft.shop.entities.User;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/****** to add additional info in token structure ******/
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        User user = (User) authentication.getPrincipal();
        Map<String, Object> map = new HashMap<>();
        map.put("full_name", user.getFullName());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(map);
        System.out.println(accessToken);
        return accessToken;
    }
}

package com.hqsoft.shop.config;

import com.hqsoft.shop.service.ClientService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
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;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import java.util.Arrays;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    private PasswordEncoder oauthClientPasswordEncoder;
    private AuthenticationManager authenticationManager;
    private ClientService clientService;

    public AuthorizationServer(PasswordEncoder oauthClientPasswordEncoder,
                               AuthenticationManager authenticationManager,
                               ClientService clientService
    ) {
        this.oauthClientPasswordEncoder = oauthClientPasswordEncoder;
        this.authenticationManager = authenticationManager;
        this.clientService = clientService;
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").passwordEncoder(oauthClientPasswordEncoder);
    }

    /******   for configuring client ******/
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(clientService);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), jwtAccessTokenConverter()));

        endpoints
                .tokenStore(tokenStore())
                .authenticationManager(authenticationManager)
                .tokenEnhancer(tokenEnhancerChain);
    }

    @Bean
    public TokenEnhancer tokenEnhancer() {
        return new CustomTokenEnhancer();
    }

    @Bean
    @Primary
    public DefaultTokenServices defaultTokenService() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /******   jwt token implementation  ******/
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        Resource resource = new ClassPathResource("keys.jks");
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, "*******".toCharArray());
        jwtAccessTokenConverter.setKeyPair(keyStoreKeyFactory.getKeyPair("mykey"));
        return jwtAccessTokenConverter;
    }
}
package com.hqsoft.shop.config;
导入com.hqsoft.shop.entities.User;
导入org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
导入org.springframework.security.oauth2.common.OAuth2AccessToken;
导入org.springframework.security.oauth2.provider.OAuth2Authentication;
导入org.springframework.security.oauth2.provider.token.TokenEnhancer;
导入java.util.HashMap;
导入java.util.Map;
/******在令牌结构中添加其他信息的步骤******/
公共类CustomTokenEnhancer实现了TokenEnhancer{
@凌驾
公共OAuth2AccessToken增强(OAuth2AccessToken accessToken,OAuth2Authentication身份验证){
User=(用户)身份验证。getPrincipal();
Map Map=newhashmap();
put(“full_name”,user.getFullName());
((DefaultOAuth2AccessToken)accessToken);
System.out.println(accessToken);
返回accessToken;
}
}
包com.hqsoft.shop.config;
导入com.hqsoft.shop.service.ClientService;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.Primary;
导入org.springframework.core.io.ClassPathResource;
导入org.springframework.core.io.Resource;
导入org.springframework.security.authentication.AuthenticationManager;
导入org.springframework.security.crypto.password.PasswordEncoder;
导入org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
导入org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
导入org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
导入org.springframework.security.oauth2.config.annotation.web.configurers.authorizationServerEndpointsConfiger;
导入org.springframework.security.oauth2.config.annotation.web.configurers.authorizationServerSecurityConfiger;
导入org.springframework.security.oauth2.provider.token.DefaultTokenServices;
导入org.springframework.security.oauth2.provider.token.TokenEnhancer;
导入org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
导入org.springframework.security.oauth2.provider.token.TokenStore;
导入org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
导入org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
导入org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
导入java.util.array;
@配置
@EnableAuthorizationServer
公共类AuthorizationServer扩展AuthorizationServerConfigurerAdapter{
专用密码编码器oauthClientPasswordEncoder;
私人AuthenticationManager AuthenticationManager;
私人客户服务;
公共授权服务器(PasswordEncoder oauthClientPasswordEncoder,
AuthenticationManager AuthenticationManager,
客户端服务客户端服务
) {
this.oauthClientPasswordEncoder=oauthClientPasswordEncoder;
this.authenticationManager=authenticationManager;
this.clientService=clientService;
}
@凌驾
public void配置(AuthorizationServerSecurityConfigurer安全){
security.tokenKeyAccess(“permitAll()”).checkTokenAccess(“isAuthenticated()”).passwordEncoder(oauthClientPasswordEncoder);
}
/******用于配置客户端******/
@凌驾
公共无效配置(ClientDetailsServiceConfigurer客户端)引发异常{
客户端。withClientDetails(客户端服务);
}
@凌驾
公共无效配置(授权服务器端点配置器端点){
TokenEnhancerCain TokenEnhancerCain=新的TokenEnhancerCain();
tokenEnhancerCain.setTokenEnhancers(Arrays.asList(tokenEnhancer(),jwtAccessTokenConverter());
public class CustomUserAuthenticationConverter extends DefaultUserAuthenticationConverter {
   @Override
    public Map<String, ?> convertUserAuthentication(Authentication authentication) {
      Map<String, Object> response = super.convertUserAuthentication(authentication);

      // Add additional info
      User user = (User) authentication.getPrincipal();
      response.put("full_name", user.getFullName());
      return response;
   }
}
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        Resource resource = new ClassPathResource("keys.jks");
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, "*******".toCharArray());
        jwtAccessTokenConverter.setKeyPair(keyStoreKeyFactory.getKeyPair("mykey"));

        // ...
        AccessTokenConverter tokenConverter = new DefaultAccessTokenConverter();
        tokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter());
        jwtAccessTokenConverter.setAccessTokenConverter(customAccessTokenConverter);
        // ...

        return jwtAccessTokenConverter;
    }
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    endpoints
            .tokenStore(tokenStore())
            .authenticationManager(authenticationManager)
            .tokenConverter(jwtAccessTokenConverter());
}