Dem*_*rvO 2 java oauth spring-mvc spring-security redis
这是我第一次使用OAuth2方法开发应用程序。我是根据某些教程开始的,并且我从此教程开始前进(http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/)。
我将应用程序部署到集群的WebSpheres,据我所知,内存中的内存将无法工作(... clients.inMemory()。withClient ...)。
我想使用Redis(也是我的第一个用法),我有点困惑如何在某些no-xml java config应用程序中设置它。
我在xml中发现了类似的问题,但是第一次尝试(Redis Token Store)仍然没有问题。有趣的是,问题所有者在此讨论了有关“ Spring-Security OAuth,即2.8.0提供RedisTokenStore”的问题,但我发现“ 2.0.12.RELEASE”是最新的mvn发行版本。
就是说,我的直截了当的问题是:如何调整以下代码以依靠Redis而不是内存?
关于如何设置RedisTokenStore的任何评论都将受到赞赏。
另外,如果很容易添加此类附加注释,那么“ .passwordEncoder”和“ .secret”之间有什么区别?下面的代码依赖于带有硬编码表达式(固定值)的“ .secret”,而我看到的几个示例中使用带有“ .frame.spring.work.security.crypto.bcrypt.BCryptPasswordEncoder”填充的“ .passwordEncoder”的jdbc似乎更有意义。我猜我使用“ .secret”还是“ .passwordEncoder”对吗?我认为secret代表固定值,而passwordEncoder代表动态值,对吗?
(使用“ .passwordEncoder”和clients.jdbc的示例https://github.com/spring-projects/spring-security-oauth/blob/master/tests/annotation/jdbc/src/main/java/demo/Application.java #L102)
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
private static String REALM="MY_OAUTH_REALM";
@Autowired
private TokenStore tokenStore;
@Autowired
private UserApprovalHandler userApprovalHandler;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("abc-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.secret("abc-secret")
.accessTokenValiditySeconds(120).//Access token is only valid for 2 minutes.
refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
.authenticationManager(authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.realm(REALM+"/client");
}
}
Run Code Online (Sandbox Code Playgroud)
如果使用Spring Boot,则将依赖项添加到pom.xml中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
使用application.properties中的适当参数设置Redis连接:
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379
Run Code Online (Sandbox Code Playgroud)
然后,将其添加到AuthorizationServerConfiguration类,您应该可以开始使用:
@Bean
public TokenStore tokenStore(RedisConnectionFactory redisConnectionFactory) {
return new RedisTokenStore(redisConnectionFactory);
}
Run Code Online (Sandbox Code Playgroud)
小智 1
这里,我搭建了一个oauth2授权服务器: https: //github.com/zth390872451/oauth2-redis-mysql \xef\xbc\x8c如果你是中国人\xef\xbc\x8c你可以阅读这篇博客。如果不是,对此我很抱歉!\ngithub\xef\xbc\x8c的这个项目我使用oauth-server作为授权服务器,它使用redis来存储accesstoken\xef\xbc\x8c你只是用来配置数据源和redis!通过复制两个类,即:AuthAuthorizeConfig和DataStoreConfig,就可以使用redis来存储token了!
\n| 归档时间: |
|
| 查看次数: |
7439 次 |
| 最近记录: |