Guest User

Untitled

a guest
Jan 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. @Configuration
  2. @EnableResourceServer
  3. public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  4.  
  5. private static final String RESOURCE_ID = "my_rest_api";
  6.  
  7. @Override
  8. public void configure(ResourceServerSecurityConfigurer resources) {
  9. resources.resourceId(RESOURCE_ID).stateless(false);
  10. }
  11.  
  12. @Override
  13. public void configure(HttpSecurity http) throws Exception {
  14. http.
  15. anonymous().disable()
  16. .requestMatchers().antMatchers("/user/**")
  17. .and().authorizeRequests()
  18. .antMatchers("/user/**").access("hasRole('ADMIN')")
  19. .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
  20. }
  21.  
  22. }
  23.  
  24. @Configuration
  25. @EnableAuthorizationServer
  26. public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  27.  
  28. @Autowired
  29. private TokenStore tokenStore;
  30.  
  31. @Autowired
  32. private UserApprovalHandler userApprovalHandler;
  33.  
  34. @Autowired
  35. @Qualifier("authenticationManagerBean")
  36. private AuthenticationManager authenticationManager;
  37.  
  38. @Autowired
  39. private AuthorizationCodeServices authorizationCodeServices;
  40.  
  41. @Override
  42. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  43.  
  44. clients.inMemory()
  45. .withClient("my-trusted-client")
  46. .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
  47. .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
  48. .scopes("read", "write", "trust")
  49. .secret("secret")
  50. .redirectUris("http://localhost:8080/clientapp/auth/callback")
  51. .autoApprove(true)
  52. .accessTokenValiditySeconds(120).//Access token is only valid for 2 minutes.
  53. refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.
  54. }
  55.  
  56. @Override
  57. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  58. endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
  59. .authenticationManager(authenticationManager).authorizationCodeServices(authorizationCodeServices);
  60. }
  61.  
  62. @Override
  63. public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
  64. oauthServer
  65. .tokenKeyAccess("permitAll()")
  66. .checkTokenAccess("isAuthenticated()");
  67. }
  68.  
  69. }
  70.  
  71. @Configuration
  72. @EnableWebSecurity
  73. public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
  74.  
  75. @Autowired
  76. private ClientDetailsService clientDetailsService;
  77.  
  78. @Autowired
  79. public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
  80. auth.inMemoryAuthentication()
  81. .withUser("bill").password("abc123").roles("ADMIN").and()
  82. .withUser("bob").password("abc123").roles("USER");
  83. }
  84.  
  85. @Override
  86. protected void configure(HttpSecurity http) throws Exception {
  87. http
  88. .csrf().disable()
  89. .anonymous().disable()
  90. .authorizeRequests()
  91. .antMatchers("/oauth/authorize", "/oauth/token").permitAll();
  92. }
  93.  
  94. @Override
  95. @Bean
  96. public AuthenticationManager authenticationManagerBean() throws Exception {
  97. return super.authenticationManagerBean();
  98. }
  99.  
  100. @Bean
  101. protected AuthorizationCodeServices authorizationCodeServices() {
  102. return new InMemoryAuthorizationCodeServices();
  103. }
  104.  
  105. @Bean
  106. public TokenStore tokenStore() {
  107. return new InMemoryTokenStore();
  108. }
  109.  
  110. @Bean
  111. @Autowired
  112. public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
  113. TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
  114. handler.setTokenStore(tokenStore);
  115. handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
  116. handler.setClientDetailsService(clientDetailsService);
  117. return handler;
  118. }
  119.  
  120. @Bean
  121. @Autowired
  122. public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
  123. TokenApprovalStore store = new TokenApprovalStore();
  124. store.setTokenStore(tokenStore);
  125. return store;
  126. }
  127.  
  128. }
  129.  
  130. <?xml version="1.0" encoding="UTF-8"?>
  131. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  132. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  133. <modelVersion>4.0.0</modelVersion>
  134.  
  135. <groupId>com.pg.oauth2.example</groupId>
  136. <artifactId>oauth2-example</artifactId>
  137. <version>0.0.1-SNAPSHOT</version>
  138. <packaging>war</packaging>
  139.  
  140. <name>oauth2-example</name>
  141. <description>OAuth2 Example</description>
  142.  
  143. <parent>
  144. <groupId>org.springframework.boot</groupId>
  145. <artifactId>spring-boot-starter-parent</artifactId>
  146. <version>1.5.9.RELEASE</version>
  147. <relativePath/> <!-- lookup parent from repository -->
  148. </parent>
  149.  
  150. <properties>
  151. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  152. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  153. <java.version>1.8</java.version>
  154. </properties>
  155.  
  156. <dependencies>
  157. <dependency>
  158. <groupId>org.springframework.boot</groupId>
  159. <artifactId>spring-boot-starter-web</artifactId>
  160. </dependency>
  161.  
  162. <dependency>
  163. <groupId>org.springframework.boot</groupId>
  164. <artifactId>spring-boot-starter-tomcat</artifactId>
  165. <scope>provided</scope>
  166. </dependency>
  167.  
  168. <dependency>
  169. <groupId>org.springframework.boot</groupId>
  170. <artifactId>spring-boot-starter-security</artifactId>
  171. </dependency>
  172.  
  173. <dependency>
  174. <groupId>org.springframework.security.oauth</groupId>
  175. <artifactId>spring-security-oauth2</artifactId>
  176. </dependency>
  177. <dependency>
  178. <groupId>org.springframework.boot</groupId>
  179. <artifactId>spring-boot-starter-test</artifactId>
  180. <scope>test</scope>
  181. </dependency>
  182. </dependencies>
  183.  
  184. <build>
  185. <plugins>
  186. <plugin>
  187. <groupId>org.springframework.boot</groupId>
  188. <artifactId>spring-boot-maven-plugin</artifactId>
  189. </plugin>
  190. </plugins>
  191. </build>
  192.  
  193.  
  194. </project>
Add Comment
Please, Sign In to add comment