Java Code Examples for org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter#setKeyPair()

The following examples show how to use org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter#setKeyPair() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthorizationServerTokenServicesConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(JwtAccessTokenConverter.class)
public JwtAccessTokenConverter accessTokenConverter() {
	Assert.notNull(this.authorization.getJwt().getKeyStore(), "keyStore cannot be null");
	Assert.notNull(this.authorization.getJwt().getKeyStorePassword(), "keyStorePassword cannot be null");
	Assert.notNull(this.authorization.getJwt().getKeyAlias(), "keyAlias cannot be null");

	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();

	Resource keyStore = this.context.getResource(this.authorization.getJwt().getKeyStore());
	char[] keyStorePassword = this.authorization.getJwt().getKeyStorePassword().toCharArray();
	KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(keyStore, keyStorePassword);

	String keyAlias = this.authorization.getJwt().getKeyAlias();
	char[] keyPassword = Optional.ofNullable(this.authorization.getJwt().getKeyPassword())
			.map(String::toCharArray).orElse(keyStorePassword);
	converter.setKeyPair(keyStoreKeyFactory.getKeyPair(keyAlias, keyPassword));

	return converter;
}
 
Example 2
Source File: ResourceServerTokenServicesConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(JwtAccessTokenConverter.class)
public JwtAccessTokenConverter accessTokenConverter() {
	Assert.notNull(this.resource.getJwt().getKeyStore(), "keyStore cannot be null");
	Assert.notNull(this.resource.getJwt().getKeyStorePassword(), "keyStorePassword cannot be null");
	Assert.notNull(this.resource.getJwt().getKeyAlias(), "keyAlias cannot be null");

	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();

	Resource keyStore = this.context.getResource(this.resource.getJwt().getKeyStore());
	char[] keyStorePassword = this.resource.getJwt().getKeyStorePassword().toCharArray();
	KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(keyStore, keyStorePassword);

	String keyAlias = this.resource.getJwt().getKeyAlias();
	char[] keyPassword = Optional.ofNullable(this.resource.getJwt().getKeyPassword()).map(String::toCharArray)
			.orElse(keyStorePassword);
	converter.setKeyPair(keyStoreKeyFactory.getKeyPair(keyAlias, keyPassword));

	return converter;
}
 
Example 3
Source File: JWTOauth2Test.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory(
            new ClassPathResource("keystore.jks"), "password".toCharArray())
            .getKeyPair("selfsigned");
    converter.setKeyPair(keyPair);
    return converter;
}
 
Example 4
Source File: Oauth2AuthorizationTokenConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * jwt 令牌 配置,非对称加密
 *
 * @return 转换器
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    final JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
    accessTokenConverter.setKeyPair(keyPair());
    return accessTokenConverter;
}
 
Example 5
Source File: OAuth2AuthorizationServerConfig.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory(
            new ClassPathResource(keystore),
            keyStorePass.toCharArray()
    ).getKeyPair(keyPairAlias);

    converter.setKeyPair(keyPair);
    return converter;
}
 
Example 6
Source File: OAuthServerConfiguration.java    From microservices-basics-spring-boot with Apache License 2.0 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
	//Keypair is the alias name -> anilkeystore.jks / password / anila
	KeyPair keyPair = new KeyStoreKeyFactory(new ClassPathResource("anilkeystore.jks"), "password".toCharArray())
			.getKeyPair("anila");
	converter.setKeyPair(keyPair);
	return converter;
}
 
Example 7
Source File: JwtServerConfiguration.java    From java-microservice with MIT License 5 votes vote down vote up
@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
    KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
            new ClassPathResource("jwt.jks"), 
            ENC_PASSWORD.toCharArray()
    );
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setKeyPair(keyStoreKeyFactory.getKeyPair("jwt"));
    return converter;
}
 
Example 8
Source File: AuthorizationConfig.java    From Using-Spring-Oauth2-to-secure-REST with MIT License 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyStoreKeyFactory keyStoreKeyFactory =
            new KeyStoreKeyFactory(
                    new ClassPathResource("mykeys.jks"),
                    "mypass".toCharArray());
    converter.setKeyPair(keyStoreKeyFactory.getKeyPair("mykeys"));
    return converter;
}
 
Example 9
Source File: UaaConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * This bean generates an token enhancer, which manages the exchange between JWT acces tokens and Authentication
 * in both directions.
 *
 * @return an access token converter configured with the authorization server's public/private keys
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory(
         new ClassPathResource(uaaProperties.getKeyStore().getName()), uaaProperties.getKeyStore().getPassword().toCharArray())
         .getKeyPair(uaaProperties.getKeyStore().getAlias());
    converter.setKeyPair(keyPair);
    return converter;
}
 
Example 10
Source File: AuthserverApplication.java    From micro-ecommerce with Apache License 2.0 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
	KeyPair keyPair = new KeyStoreKeyFactory(new ClassPathResource("keystore.jks"), "foobar".toCharArray())
			.getKeyPair("test");
	converter.setKeyPair(keyPair);
	return converter;
}
 
Example 11
Source File: OAuth2Configuration.java    From spring-boot-oauth2-jwt with MIT License 5 votes vote down vote up
@Bean
protected JwtAccessTokenConverter jwtAccessTokenConverter() {
	JwtAccessTokenConverter converter = new CustomTokenEnhancer();
	converter.setKeyPair(
			new KeyStoreKeyFactory(new ClassPathResource("jwt.jks"), "password".toCharArray()).getKeyPair("jwt"));
	return converter;
}
 
Example 12
Source File: OAuth2Config.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
/**
 * This bean generates an token enhancer, which manages the exchange between JWT acces tokens and Authentication
 * in both direction.
 *
 * @return an access token converter configured with the authorization server's public/private keys
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory(
            new ClassPathResource("keystore.jks"), "password".toCharArray())
            .getKeyPair("selfsigned");
    converter.setKeyPair(keyPair);
    return converter;
}
 
Example 13
Source File: OAuth2Configuration.java    From spring-boot-2-oauth2-authorization-jwt with MIT License 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
	JwtAccessTokenConverter converter = new CustomTokenEnhancer();
	converter.setKeyPair(
			new KeyStoreKeyFactory(new ClassPathResource("jwt.jks"), "password".toCharArray()).getKeyPair("jwt"));
	return converter;
}
 
Example 14
Source File: JwtTokenConfigration.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * JWT Token 生成转换器(加密方式以及加密的Token中存放哪些信息)
 *
 * @return
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory
            (oauth2Properties.getKeyStore().getLocation(), oauth2Properties.getKeyStore().getSecret().toCharArray())
            .getKeyPair(oauth2Properties.getKeyStore().getAlias());
    converter.setKeyPair(keyPair);
    converter.setAccessTokenConverter(new CustomerAccessTokenConverter());
    return converter;
}
 
Example 15
Source File: OauthAuthorizationServerConfig.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 配置AccessToken加密方式
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(CustomUserAuthenticationConverter customUserAuthenticationConverter) {
	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
	KeyPair keyPair = new KeyStoreKeyFactory(
			keyProperties.getKeyStore().getLocation(),
			keyProperties.getKeyStore().getSecret().toCharArray()).getKeyPair(
			keyProperties.getKeyStore().getAlias(),
			keyProperties.getKeyStore().getPassword().toCharArray());
	converter.setKeyPair(keyPair);
	//配置自定义的CustomUserAuthenticationConverter
	DefaultAccessTokenConverter accessTokenConverter = (DefaultAccessTokenConverter) converter.getAccessTokenConverter();
	accessTokenConverter.setUserTokenConverter(customUserAuthenticationConverter);
	return converter;
}
 
Example 16
Source File: UaaConfiguration.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * This bean generates an token enhancer, which manages the exchange between JWT acces tokens and Authentication
 * in both directions.
 *
 * @return an access token converter configured with the authorization server's public/private keys
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory(
         new ClassPathResource(uaaProperties.getKeyStore().getName()), uaaProperties.getKeyStore().getPassword().toCharArray())
         .getKeyPair(uaaProperties.getKeyStore().getAlias());
    converter.setKeyPair(keyPair);
    return converter;
}
 
Example 17
Source File: OAuth2AuthorizationServerConfig.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory(
            new ClassPathResource("keys/jwtConverterStore.jks"),
            keyStorePass.toCharArray()
    ).getKeyPair(keyPairAlias);

    converter.setKeyPair(keyPair);
    return converter;
}
 
Example 18
Source File: Oauth2AuthorizationServerConfig.java    From spring-security-oauth2-demo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 令牌转换器,(非)对称密钥加密
 *
 * @return JwtAccessTokenConverter
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    //  对称密钥加密
    //  converter.setSigningKey("oauth2");
    //  非对称密钥加密
    KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
            new ClassPathResource("oauth2.jks"), "123456".toCharArray());
    converter.setKeyPair(keyStoreKeyFactory.getKeyPair("oauth2"));
    return converter;
}
 
Example 19
Source File: AuthJwtTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory
            (keyProperties.getKeyStore().getLocation(), keyProperties.getKeyStore().getSecret().toCharArray())
            .getKeyPair(keyProperties.getKeyStore().getAlias());
    converter.setKeyPair(keyPair);
    DefaultAccessTokenConverter tokenConverter = (DefaultAccessTokenConverter)converter.getAccessTokenConverter();
    tokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter());
    return converter;
}
 
Example 20
Source File: AuthorizationServerConfiguration.java    From spring-boot-demo with MIT License 5 votes vote down vote up
@Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
//    converter.setSigningKey("123");
        KeyStoreKeyFactory keyStoreKeyFactory =
                new KeyStoreKeyFactory(new ClassPathResource("mytest.jks"), "mypass".toCharArray());
        converter.setKeyPair(keyStoreKeyFactory.getKeyPair("mytest"));
        converter.setAccessTokenConverter(new CustomerAccessTokenConverter());
        return converter;
    }