Spring cloud security Oauth 2マルチノードクラスタの変更が必要な構成



最近Spring cloudマイクロサービスの構築を学び、SpringのOAuth 2を単点登録サーバとしてzulで統一ルーティングを行うため、認証サービス(Authentication Server)側はマルチインスタンスでクラスタ配置を行うことができ、クラスタ配置時に認証サービス(Authentication Server)を以下のように構成する必要がある.
1.redisによるAccessTokenの格納
 
@Configuration
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
        @Autowired
	private AuthenticationManager authenticationManager;
        @Autowired
	private RedisConnectionFactory redisConnectionFactory;
        @Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		endpoints.authenticationManager(authenticationManager).accessTokenConverter(jwtAccessTokenConverter());
		endpoints.tokenStore(tokenStore());
	}
	

	@Bean
	public TokenStore tokenStore() {
		return new RedisTokenStore(redisConnectionFactory);
	}


}

2、Authentication codeを共有する
 
現在springでは、ライセンスコードAuthentication codeを格納するメモリとデータベースの2つの方法が用意されています.複数のインスタンス間の共有を実現するには、AuthenTicationCodeをデータに格納するか、関連構成は
 
        @Bean
	public AuthorizationCodeServices authorizationCodeServices(){
		return new JdbcAuthorizationCodeServices(dataSource);
	}

        @Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		endpoints.authenticationManager(authenticationManager).accessTokenConverter(jwtAccessTokenConverter());
		endpoints.tokenStore(tokenStore());
		endpoints.authorizationCodeServices(authorizationCodeServices());
	}

データベースにテーブルauth_を作成する必要がありますcode
 
 
CREATE TABLE `oauth_code` (
  `code` varchar(1024) DEFAULT NULL,
  `authentication` blob
)

AuthorizationCodeServicesインプリメンテーションクラスをカスタマイズしてauth_コードはredisに格納されます
 
 
public class RedisAuthenticationCodeServices extends RandomValueAuthorizationCodeServices {
	private static Logger log = Logger.getLogger(RedisAuthenticationCodeServices.class);
	private static final String AUTH_CODE_KEY = "auth_code";
	private RedisConnectionFactory connectionFactory;

	public RedisAuthenticationCodeServices(RedisConnectionFactory connectionFactory) {
		Assert.notNull(connectionFactory, "RedisConnectionFactory required");
		this.connectionFactory = connectionFactory;
	}

	@Override
	protected OAuth2Authentication remove(String code) {
		RedisConnection conn = getConnection();
		try {
			OAuth2Authentication authentication = null;

			try {
				authentication = SerializationUtils
						.deserialize(conn.hGet(AUTH_CODE_KEY.getBytes("utf-8"), code.getBytes("utf-8")));
			} catch (Exception e) {
				return null;
			}

			if (null != authentication) {
				conn.hDel(AUTH_CODE_KEY.getBytes("utf-8"), code.getBytes("utf-8"));
			}

			return authentication;
		} catch (Exception e) {
			return null;
		} finally {
			conn.close();
		}
	}

	@Override
	protected void store(String code, OAuth2Authentication authentication) {
		RedisConnection conn = getConnection();
		try {
			conn.hSet(AUTH_CODE_KEY.getBytes("utf-8"), code.getBytes("utf-8"),
					SerializationUtils.serialize(authentication));
		} catch (Exception e) {
			log.error("  authentication code   ", e);
		} finally {
			conn.close();
		}

	}

	private RedisConnection getConnection() {
		return connectionFactory.getConnection();
	}

}

3、共有セッション
 
OAuth 2サービスは認証コードを申請する際にまずユーザーがログインしているかどうかを判断し、ログインしていない場合はログインページに起動するので、異なるインスタンス間でセッションを共有する必要があります.Springのセッションで簡単にセッション共有を実現できますか.まずjarパッケージを導入し、mavenの構成は以下の通りです.
 

	org.springframework.boot
	spring-boot-starter-data-redis


	org.springframework.session
	spring-session

@EnableRedisHttpSession注記でsession共有をオンにします.