首先(如果我已经正确理解)安全实现是在代理上?因为代理必须只有两件事要做:过滤和路由......
我的microservces应用程序流程,我已经实现了如下图所示:
流程应该是这样的: https://tools.ietf.org/html/rfc6749#page-7
关于流程的简短介绍:
在AccountServices中,您必须实现一个配置类来解码access_token并检查用户是否有权访问所请求的资源
您也可以在这里找到关于在Spring中实现的OAuth2框架的优秀文档: http://projects.spring.io/spring-security-oauth/docs/oauth2.html
一些代码:
的 在AuthService上 强>
@Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { public final static String RESOURCE_ID = "server-resource"; @Value("${jwt.publicKey}") private String publicKey; @Value("${jwt.privateKey}") private String privateKey; @Autowired private AuthenticationManager authenticationManager; @Bean public TokenStore tokenStore() { return new JwtTokenStore(accessTokenConverter()); } @Bean public JwtAccessTokenConverter accessTokenConverter() { JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); converter.setVerifierKey(publicKey); converter.setSigningKey(privateKey); return converter; } @Bean public TokenEnhancer customTokenEnhancer() { return new CustomTokenEnhancer(); } @Override public void configure(ClientDetailsServiceConfigurer client) throws Exception { client.inMemory() .withClient("client") .secret("clientsecret") .scopes("read", "write") .resourceIds("user") .authorizedGrantTypes("password", "refresh_token", "authorization_code") .authorities("ROLE_TRUSTED_CLIENT") .accessTokenValiditySeconds(tokenExpire) // one day available .refreshTokenValiditySeconds(refreshExpire); } @Override public void configure(AuthorizationServerSecurityConfigurer server) throws Exception { server .tokenKeyAccess("hasAuthority('ROLE_TRUSTED_CLIENT')") .checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')"); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints .tokenStore(tokenStore()) .authenticationManager(authenticationManager) .accessTokenConverter(accessTokenConverter()); } }
的 关于公钥和私钥: 强> 该 私钥 必须仅由AuthServer知道,并且必须在包括AuthService的任何服务中传递公钥。您可以在此处生成公钥和私钥: http://travistidwell.com/jsencrypt/demo/ 并添加这些键 application.yml 文件并传入配置类 @Value 。
@Value
的 在资源服务器上 强>
@Configuration @EnableResourceServer @EnableGlobalMethodSecurity(prePostEnabled = true) public class OAuth2ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Value("${jwt.publicKey}") private String publicKey; @Bean public TokenStore tokenStore() { return new JwtTokenStore(jwtAccessTokenConverter()); } @Bean protected JwtAccessTokenConverter jwtAccessTokenConverter() { JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); converter.setVerifierKey(publicKey); return converter; } @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources .tokenStore(tokenStore()) .resourceId("user"); } @Override public void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests().antMatchers("/**").permitAll(); } }
您唯一需要做的就是为资源服务(AccountService)创建一个配置类来解码access_token并检查用户是否有ROLE来做某事......在这里你必须只传递 的 公钥 强> 以同样的方式 application.yml 文件。
关于 @EnableGlobalMethodSecurity(prePostEnabled = true) 您可以添加的注释 @preauthorize 控制器方法的注释。
@EnableGlobalMethodSecurity(prePostEnabled = true)
@preauthorize