Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ install: ./mvnw -U install --quiet -DskipTests=true -P bootstrap
script:
- jdk_switcher use openjdk7
- ./mvnw clean test -P bootstrap
# - jdk_switcher use oraclejdk8
# - ./mvnw -U clean test -P spring5
- jdk_switcher use oraclejdk8
- ./mvnw -f spring-security-oauth2 -U clean test -P spring5
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<commons-codec.version>1.9</commons-codec.version>
<spring.version>4.0.9.RELEASE</spring.version>
<spring.security.version>3.2.10.RELEASE</spring.security.version>
<spring.data.redis.version>1.5.0.RELEASE</spring.data.redis.version>
<redis.clients.version>2.6.3</redis.clients.version>
<java.version>1.6</java.version>
</properties>

Expand Down Expand Up @@ -165,8 +167,10 @@
<profile>
<id>spring5</id>
<properties>
<spring.version>5.0.0.BUILD-SNAPSHOT</spring.version>
<spring.security.version>5.0.0.BUILD-SNAPSHOT</spring.security.version>
<spring.version>5.0.4.RELEASE</spring.version>
<spring.security.version>5.0.3.RELEASE</spring.security.version>
<spring.data.redis.version>2.0.5.RELEASE</spring.data.redis.version>
<redis.clients.version>2.9.0</redis.clients.version>
</properties>
<repositories>
<repository>
Expand Down
4 changes: 2 additions & 2 deletions spring-security-oauth2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.0.RELEASE</version>
<version>${spring.data.redis.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.3</version>
<version>${redis.clients.version}</version>
<optional>true</optional>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void close() {
}

public int getRawStatusCode() throws IOException {
return response.getRawStatusCode();
return this.getStatusCode().value();
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package org.springframework.security.oauth2.provider.token.store.redis;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
Expand All @@ -15,6 +9,15 @@
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author efenderbosch
Expand All @@ -31,14 +34,23 @@ public class RedisTokenStore implements TokenStore {
private static final String CLIENT_ID_TO_ACCESS = "client_id_to_access:";
private static final String UNAME_TO_ACCESS = "uname_to_access:";

private static final boolean springDataRedis_2_0 = ClassUtils.isPresent(
"org.springframework.data.redis.connection.RedisStandaloneConfiguration",
RedisTokenStore.class.getClassLoader());

private final RedisConnectionFactory connectionFactory;
private AuthenticationKeyGenerator authenticationKeyGenerator = new DefaultAuthenticationKeyGenerator();
private RedisTokenStoreSerializationStrategy serializationStrategy = new JdkSerializationStrategy();

private String prefix = "";

private Method redisConnectionSet_2_0;

public RedisTokenStore(RedisConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
if (springDataRedis_2_0) {
this.loadRedisConnectionMethods_2_0();
}
}

public void setAuthenticationKeyGenerator(AuthenticationKeyGenerator authenticationKeyGenerator) {
Expand All @@ -53,6 +65,11 @@ public void setPrefix(String prefix) {
this.prefix = prefix;
}

private void loadRedisConnectionMethods_2_0() {
this.redisConnectionSet_2_0 = ReflectionUtils.findMethod(
RedisConnection.class, "set", byte[].class, byte[].class);
}

private RedisConnection getConnection() {
return connectionFactory.getConnection();
}
Expand Down Expand Up @@ -157,9 +174,19 @@ public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authe
RedisConnection conn = getConnection();
try {
conn.openPipeline();
conn.set(accessKey, serializedAccessToken);
conn.set(authKey, serializedAuth);
conn.set(authToAccessKey, serializedAccessToken);
if (springDataRedis_2_0) {
try {
this.redisConnectionSet_2_0.invoke(conn, accessKey, serializedAccessToken);
this.redisConnectionSet_2_0.invoke(conn, authKey, serializedAuth);
this.redisConnectionSet_2_0.invoke(conn, authToAccessKey, serializedAccessToken);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} else {
conn.set(accessKey, serializedAccessToken);
conn.set(authKey, serializedAuth);
conn.set(authToAccessKey, serializedAccessToken);
}
if (!authentication.isClientOnly()) {
conn.rPush(approvalKey, serializedAccessToken);
}
Expand All @@ -177,9 +204,18 @@ public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authe
byte[] refresh = serialize(token.getRefreshToken().getValue());
byte[] auth = serialize(token.getValue());
byte[] refreshToAccessKey = serializeKey(REFRESH_TO_ACCESS + token.getRefreshToken().getValue());
conn.set(refreshToAccessKey, auth);
byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + token.getValue());
conn.set(accessToRefreshKey, refresh);
if (springDataRedis_2_0) {
try {
this.redisConnectionSet_2_0.invoke(conn, refreshToAccessKey, auth);
this.redisConnectionSet_2_0.invoke(conn, accessToRefreshKey, refresh);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} else {
conn.set(refreshToAccessKey, auth);
conn.set(accessToRefreshKey, refresh);
}
if (refreshToken instanceof ExpiringOAuth2RefreshToken) {
ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken) refreshToken;
Date expiration = expiringRefreshToken.getExpiration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
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;
Expand Down Expand Up @@ -188,5 +190,10 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
}
};
}

@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
Expand Down Expand Up @@ -253,6 +255,10 @@ public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

@Configuration
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
}
}

Expand All @@ -275,6 +281,10 @@ public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws E
}
@Configuration
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import org.junit.Test;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.oauth2.common.*;
import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.RequestTokenFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.TokenStoreBaseTests;
import org.springframework.util.ClassUtils;
import redis.clients.jedis.JedisShardInfo;

import java.util.Collection;
Expand All @@ -32,8 +37,18 @@ public TokenStore getTokenStore() {

@Before
public void setup() throws Exception {
JedisShardInfo shardInfo = new JedisShardInfo("localhost");
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(shardInfo);
boolean springDataRedis_2_0 = ClassUtils.isPresent(
"org.springframework.data.redis.connection.RedisStandaloneConfiguration",
this.getClass().getClassLoader());

JedisConnectionFactory connectionFactory;
if (springDataRedis_2_0) {
connectionFactory = new JedisConnectionFactory();
} else {
JedisShardInfo shardInfo = new JedisShardInfo("localhost");
connectionFactory = new JedisConnectionFactory(shardInfo);
}

tokenStore = new RedisTokenStore(connectionFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

<authentication-manager id="usersAuthenticationManager">
<authentication-provider>
<password-encoder ref="passwordEncoder" />
<user-service>
<user name="acme" password="password" authorities="ROLE_USER" />
</user-service>
Expand All @@ -65,4 +66,6 @@
<oauth2:password authentication-manager-ref="usersAuthenticationManager" />
</oauth2:authorization-server>

<b:bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method="getInstance" />

</b:beans>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
</b:bean>

<authentication-manager id="clientsAuthenticationManager">
<authentication-provider user-service-ref="clientDetailsUserDetailsService" />
<authentication-provider user-service-ref="clientDetailsUserDetailsService">
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>

<b:bean id="clientDetailsUserDetailsService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
Expand All @@ -46,6 +48,7 @@

<authentication-manager id="usersAuthenticationManager">
<authentication-provider>
<password-encoder ref="passwordEncoder" />
<user-service>
<user name="acme" password="password" authorities="ROLE_USER" />
</user-service>
Expand All @@ -59,4 +62,6 @@
<oauth2:password authentication-manager-ref="usersAuthenticationManager" />
</oauth2:authorization-server>

<b:bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method="getInstance" />

</b:beans>