new demo classes for oauth

This commit is contained in:
eugenp 2016-03-01 20:57:38 +02:00
parent f1d304ce81
commit 54131bcb2c
5 changed files with 101 additions and 12 deletions

View File

@ -9,8 +9,10 @@ import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecur
@Configuration @Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override @Override
protected MethodSecurityExpressionHandler createExpressionHandler() { protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler(); return new OAuth2MethodSecurityExpressionHandler();
} }
} }

View File

@ -20,11 +20,14 @@ import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
@PropertySource({ "classpath:persistence.properties" }) @PropertySource({ "classpath:persistence.properties" })
@EnableResourceServer @EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter { public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired @Autowired
private Environment env; private Environment env;
//
@Override @Override
public void configure(HttpSecurity http) throws Exception { public void configure(final HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)

View File

@ -0,0 +1,31 @@
package org.baeldung.config;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
//@Configuration
//@EnableResourceServer
public class OAuth2ResourceServerConfigDemo extends ResourceServerConfigurerAdapter {
//
@Override
public void configure(final HttpSecurity http) throws Exception {
// @formatter:off
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and().authorizeRequests().anyRequest().authenticated();
;
// @formatter:on
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
}

View File

@ -38,41 +38,42 @@ public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigur
@Value("classpath:schema.sql") @Value("classpath:schema.sql")
private Resource schemaScript; private Resource schemaScript;
//
@Override @Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()"); oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
} }
@Override @Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception { public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off
// @formatter:off
clients.jdbc(dataSource()) clients.jdbc(dataSource())
.withClient("sampleClientId") .withClient("sampleClientId")
.authorizedGrantTypes("implicit") .authorizedGrantTypes("implicit")
.scopes("read","write","foo","bar") .scopes("read","write","foo","bar")
.autoApprove(false) .autoApprove(false)
.accessTokenValiditySeconds(3600) .accessTokenValiditySeconds(3600)
.and() .and()
.withClient("fooClientIdPassword") .withClient("fooClientIdPassword")
.secret("secret") .secret("secret")
.authorizedGrantTypes("password","authorization_code", "refresh_token") .authorizedGrantTypes("password","authorization_code", "refresh_token")
.scopes("foo","read","write") .scopes("foo","read","write")
.accessTokenValiditySeconds(3600) // 1hour .accessTokenValiditySeconds(3600) // 1 hour
.refreshTokenValiditySeconds(2592000) // 30days .refreshTokenValiditySeconds(2592000) // 30 days
.and() .and()
.withClient("barClientIdPassword") .withClient("barClientIdPassword")
.secret("secret") .secret("secret")
.authorizedGrantTypes("password","authorization_code", "refresh_token") .authorizedGrantTypes("password","authorization_code", "refresh_token")
.scopes("bar","read","write") .scopes("bar","read","write")
.accessTokenValiditySeconds(3600) // 1hour .accessTokenValiditySeconds(3600) // 1 hour
.refreshTokenValiditySeconds(2592000) // 30days .refreshTokenValiditySeconds(2592000) // 30 days
; ;
} // @formatter:on
// @formatter:on
}
@Override @Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager); endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);
} }
@ -104,4 +105,5 @@ public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigur
public TokenStore tokenStore() { public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource()); return new JdbcTokenStore(dataSource());
} }
} }

View File

@ -0,0 +1,51 @@
package org.baeldung.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
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.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
//@Configuration
//@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfigDemo extends AuthorizationServerConfigurerAdapter {
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
//
@Override
public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off
clients.inMemory()
.withClient("fooClientIdPassword")
.secret("secret")
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.scopes("foo", "read", "write")
.accessTokenValiditySeconds(3600) // 1 hour
.refreshTokenValiditySeconds(2592000) // 30 days
;
} // @formatter:on
@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
}