Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. <groupId>com.oreilly.cloud</groupId>
  2. <artifactId>spring-microservices-oauth-server</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5.  
  6. <name>spring-microservices-oauth-server</name>
  7. <description>Demo project for Spring Boot</description>
  8.  
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.5.7.RELEASE</version>
  13. <relativePath/> <!-- lookup parent from repository -->
  14. </parent>
  15.  
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. <spring-cloud.version>Dalston.SR3</spring-cloud.version>
  21. </properties>
  22.  
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-oauth2</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-jdbc</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-security</artifactId>
  35. </dependency>
  36.  
  37. <dependency>
  38. <groupId>org.hsqldb</groupId>
  39. <artifactId>hsqldb</artifactId>
  40. <scope>runtime</scope>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. </dependency>
  47. </dependencies>
  48.  
  49. <dependencyManagement>
  50. <dependencies>
  51. <dependency>
  52. <groupId>org.springframework.cloud</groupId>
  53. <artifactId>spring-cloud-dependencies</artifactId>
  54. <version>${spring-cloud.version}</version>
  55. <type>pom</type>
  56. <scope>import</scope>
  57. </dependency>
  58. </dependencies>
  59. </dependencyManagement>
  60.  
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>
  69.  
  70. <repositories>
  71. <repository>
  72. <id>spring-snapshots</id>
  73. <name>Spring Snapshots</name>
  74. <url>https://repo.spring.io/snapshot</url>
  75. <snapshots>
  76. <enabled>true</enabled>
  77. </snapshots>
  78. </repository>
  79. <repository>
  80. <id>spring-milestones</id>
  81. <name>Spring Milestones</name>
  82. <url>https://repo.spring.io/milestone</url>
  83. <snapshots>
  84. <enabled>false</enabled>
  85. </snapshots>
  86. </repository>
  87. </repositories>
  88.  
  89. package com.oreilly.cloud;
  90.  
  91. import org.springframework.boot.SpringApplication;
  92. import org.springframework.boot.autoconfigure.SpringBootApplication;
  93. import org.springframework.security.access.prepost.PreAuthorize;
  94. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  95. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  96. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  97. import org.springframework.web.bind.annotation.RequestMapping;
  98. import org.springframework.web.bind.annotation.RestController;
  99.  
  100. @SpringBootApplication
  101. @EnableAuthorizationServer
  102. @EnableResourceServer
  103. @RestController
  104. @EnableGlobalMethodSecurity(prePostEnabled=true)
  105. public class SpringMicroservicesOauthServerApplication {
  106.  
  107. @RequestMapping("/resource/endpoint")
  108. @PreAuthorize("hasRole('ADMIN')")
  109. public String endpoint(){
  110. return "This message is protected by the resource server.";
  111. }
  112.  
  113. public static void main(String[] args) {
  114. SpringApplication.run(SpringMicroservicesOauthServerApplication.class, args);
  115. }
  116. }
  117.  
  118. package com.oreilly.cloud;
  119.  
  120. import org.springframework.beans.factory.annotation.Autowired;
  121. import org.springframework.context.annotation.Configuration;
  122. import org.springframework.security.authentication.AuthenticationManager;
  123. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  124. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  125. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  126.  
  127. @Configuration
  128. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  129.  
  130. @Autowired
  131. private AuthenticationManager authManager;
  132.  
  133. @Override
  134. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  135. endpoints.authenticationManager(authManager);
  136. }
  137.  
  138. @Override
  139. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  140. clients.inMemory().withClient("webapp").secret("websecret").authorizedGrantTypes("password")
  141. .scopes("read,write,trust");
  142. }
  143.  
  144. }
  145.  
  146. package com.oreilly.cloud;
  147.  
  148. import org.springframework.context.annotation.Bean;
  149. import org.springframework.context.annotation.Configuration;
  150. import org.springframework.security.authentication.AuthenticationManager;
  151. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  152. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  153.  
  154. @Configuration
  155. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  156.  
  157. @Bean
  158. public AuthenticationManager authenticationManagerBean() throws Exception {
  159. return super.authenticationManagerBean();
  160. }
  161.  
  162. @Override
  163. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  164. auth.inMemoryAuthentication().withUser("user1").password("password1").roles("USER").and().withUser("admin")
  165. .password("password2").roles("ADMIN");
  166. }
  167.  
  168. }
  169.  
  170. server.port=9090
  171.  
  172. {
  173. "access_token": "2d632e54-17c3-41f7-af3b-935ca3022d78",
  174. "token_type": "bearer",
  175. "expires_in": 43199,
  176. "scope": "read,write,trust"
  177. }
  178.  
  179. <html>
  180. <head>
  181. <title>Login Page</title>
  182. </head>
  183. <body onload='document.f.username.focus();'>
  184. <h3>Login with Username and Password</h3>
  185. <form name='f' action='/login' method='POST'>
  186. <table>
  187. <tr>
  188. <td>User:</td>
  189. <td>
  190. <input type='text' name='username' value=''>
  191. </td>
  192. </tr>
  193. <tr>
  194. <td>Password:</td>
  195. <td>
  196. <input type='password' name='password'/>
  197. </td>
  198. </tr>
  199. <tr>
  200. <td colspan='2'>
  201. <input name="submit" type="submit" value="Login"/>
  202. </td>
  203. </tr>
  204. <input name="_csrf" type="hidden" value="8dbc1c38-6f89-43c5-a8f8-797c920722a1" />
  205. </table>
  206. </form>
  207. </body>
  208. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement