@Configurationpublic class OAuth2Configuration {@SpringBootApplication@RestController@EnableResourceServer@Configuration@EnableAuthorizationServerprotected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter implements EnvironmentAware {private static final String ENV_OAUTH = "authentication.oauth.";private static final String PROP_CLIENTID = "clientid";private static final String PROP_SECRET = "secret";private static final String PROP_TOKEN_VALIDITY_SECONDS = "tokenValidityInSeconds";private RelaxedPropertyResolver propertyResolver;@Autowiredprivate DataSource dataSource;@Beanpublic TokenStore tokenStore() {return new JdbcTokenStore(dataSource);}// @Autowired// @Qualifier("authenticationManagerBean")// private AuthenticationManager authenticationManager;@Autowired@Qualifier("daoAuhthenticationOauthProvider")private AuthenticationProvider daoAuhthenticationOauthProvider;@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints)throws Exception {// @formatter:offendpoints.tokenStore(tokenStore()).authenticationManager(new AuthenticationManager(){@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {// TODO Auto-generated method stubreturn daoAuhthenticationOauthProvider.authenticate(authentication);}});// @formatter:on}@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient(propertyResolver.getProperty(PROP_CLIENTID)).scopes("read", "write").authorities(Authorities.ROLE_CHANNEL.name()).authorizedGrantTypes("password", "refresh_token").secret(propertyResolver.getProperty(PROP_SECRET)).accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 1800));}@Overridepublic void setEnvironment(Environment environment) {this.propertyResolver = new RelaxedPropertyResolver(environment, ENV_OAUTH);}@Configuration@EnableResourceServerprotected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {@Overridepublic void configure(HttpSecurity http) throws Exception {http.antMatcher("/api/dev/**").authorizeRequests().anyRequest().hasRole("DEVELEPOR").and().antMatcher("/api/channel/**").authorizeRequests().anyRequest().hasRole("CHANNEL");}}}}
联系客服