Advertisement
Guest User

Untitled

a guest
May 10th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.05 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  5. xmlns:oauth2="http://www.springframework.org/schema/security/oauth2"
  6. xmlns:security="http://www.springframework.org/schema/security"
  7. xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
  8. http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  11. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd">
  12.  
  13. <context:annotation-config base-package="hello.*"></context:annotation-config>
  14. <context:component-scan base-package="hello.*" />
  15. <jpa:repositories base-package="hello.*"></jpa:repositories>
  16.  
  17. <bean id="dataSource"
  18. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  19. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  20. <property name="url" value="jdbc:mysql://localhost:3306/test20"></property>
  21. <property name="username" value="root"></property>
  22. <property name="password" value=""></property>
  23. </bean>
  24. <bean id="persistenceUnitManager"
  25. class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
  26. <property name="persistenceXmlLocations">
  27. <list>
  28. <value>classpath*:META-INF/persistence.xml</value>
  29. </list>
  30. </property>
  31. <property name="defaultDataSource" ref="dataSource"></property>
  32. </bean>
  33. <bean id="entityManagerFactory"
  34. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  35. <property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
  36. <property name="persistenceUnitName" value="MY_P"></property>
  37. </bean>
  38. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  39. <property name="entityManagerFactory" ref="entityManagerFactory"></property>
  40. </bean>
  41.  
  42. <?xml version="1.0" encoding="UTF-8"?>
  43. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
  44. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  46.  
  47. <!-- The definition of the Root Spring Container shared by all Servlets
  48. and Filters -->
  49. <context-param>
  50. <param-name>contextConfigLocation</param-name>
  51. <param-value>classpath*:applicationContext.xml</param-value>
  52. </context-param>
  53.  
  54. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  55. <listener>
  56. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  57. </listener>
  58.  
  59. <!-- Processes application requests -->
  60. <servlet>
  61. <servlet-name>appServlet</servlet-name>
  62. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  63. <init-param>
  64. <param-name>contextConfigLocation</param-name>
  65. <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  66. </init-param>
  67. <load-on-startup>1</load-on-startup>
  68. </servlet>
  69.  
  70. <servlet-mapping>
  71. <servlet-name>appServlet</servlet-name>
  72. <url-pattern>/</url-pattern>
  73. </servlet-mapping>
  74. </web-app>
  75.  
  76. package hello;
  77.  
  78. import org.springframework.beans.factory.annotation.Autowired;
  79. import org.springframework.beans.factory.annotation.Qualifier;
  80. import org.springframework.context.annotation.Bean;
  81. import org.springframework.context.annotation.Configuration;
  82. import org.springframework.context.annotation.Primary;
  83. import org.springframework.security.authentication.AuthenticationManager;
  84. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  85. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  86. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  87. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  88. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  89. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  90. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  91. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  92. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  93. import org.springframework.security.oauth2.provider.token.TokenStore;
  94. import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
  95.  
  96. @Configuration
  97. public class OAuth2ServerConfiguration {
  98.  
  99. private static final String RESOURCE_ID = "restservice";
  100.  
  101. @Configuration
  102. @EnableResourceServer
  103. protected static class ResourceServerConfiguration extends
  104. ResourceServerConfigurerAdapter {
  105.  
  106. @Override
  107. public void configure(ResourceServerSecurityConfigurer resources) {
  108. // @formatter:off
  109. resources
  110. .resourceId(RESOURCE_ID);
  111. // @formatter:on
  112. }
  113.  
  114. @Override
  115. public void configure(HttpSecurity http) throws Exception {
  116. // @formatter:off
  117. http
  118. .authorizeRequests()
  119. .antMatchers("/users").hasRole("ADMIN")
  120. .antMatchers("/greeting").authenticated();
  121. // @formatter:on
  122. }
  123.  
  124. }
  125.  
  126. @Configuration
  127. @EnableAuthorizationServer
  128. protected static class AuthorizationServerConfiguration extends
  129. AuthorizationServerConfigurerAdapter {
  130.  
  131. private TokenStore tokenStore = new InMemoryTokenStore();
  132.  
  133. @Autowired
  134. @Qualifier("authenticationManagerBean")
  135. private AuthenticationManager authenticationManager;
  136.  
  137. @Autowired
  138. private CustomUserDetailsService userDetailsService;
  139.  
  140. @Override
  141. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  142. throws Exception {
  143. // @formatter:off
  144. endpoints
  145. .tokenStore(this.tokenStore)
  146. .authenticationManager(this.authenticationManager)
  147. .userDetailsService(userDetailsService).prefix("/greeting");
  148. // @formatter:on
  149. }
  150.  
  151. @Override
  152. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  153. // @formatter:off
  154. clients
  155. .inMemory()
  156. .withClient("clientapp")
  157. .authorizedGrantTypes("password", "refresh_token")
  158. .authorities("USER")
  159. .scopes("read", "write")
  160. .resourceIds(RESOURCE_ID)
  161. .secret("123456");
  162. // @formatter:on
  163. }
  164.  
  165. @Bean
  166. @Primary
  167. public DefaultTokenServices tokenServices() {
  168. DefaultTokenServices tokenServices = new DefaultTokenServices();
  169. tokenServices.setSupportRefreshToken(true);
  170. tokenServices.setTokenStore(this.tokenStore);
  171. return tokenServices;
  172. }
  173.  
  174. }
  175.  
  176. }
  177.  
  178. package hello;
  179.  
  180. import java.util.concurrent.atomic.AtomicLong;
  181.  
  182. import org.springframework.security.core.annotation.AuthenticationPrincipal;
  183. import org.springframework.web.bind.annotation.RequestMapping;
  184. import org.springframework.web.bind.annotation.RestController;
  185.  
  186. import hello.data.User;
  187.  
  188. @RestController
  189. public class GreetingController {
  190.  
  191. private static final String template = "Hello, %s!";
  192.  
  193. private final AtomicLong counter = new AtomicLong();
  194.  
  195. @RequestMapping("/greeting")
  196. public Greeting greeting(@AuthenticationPrincipal User user) {
  197. return new Greeting(counter.incrementAndGet(),
  198. String.format(template, user.getName()));
  199. }
  200.  
  201. }
  202.  
  203. package hello;
  204.  
  205. import org.springframework.beans.factory.annotation.Autowired;
  206. import org.springframework.context.annotation.Bean;
  207. import org.springframework.context.annotation.Configuration;
  208. import org.springframework.security.authentication.AuthenticationManager;
  209. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  210. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  211. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  212.  
  213. @Configuration
  214. @EnableWebSecurity
  215. public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
  216.  
  217. @Autowired
  218. private CustomUserDetailsService userDetailsService;
  219.  
  220. @Override
  221. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  222. auth.userDetailsService(userDetailsService);
  223. }
  224.  
  225. @Override
  226. @Bean
  227. public AuthenticationManager authenticationManagerBean() throws Exception {
  228. return super.authenticationManagerBean();
  229. }
  230. }
  231.  
  232. INFO : org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping - Mapped "{[/oauth/authorize],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(java.util.Map<java.lang.String, java.lang.Object>,java.util.Map<java.lang.String, java.lang.String>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)
  233. INFO : org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping - Mapped "{[/oauth/authorize],methods=[POST],params=[user_oauth_approval],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.View org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.approveOrDeny(java.util.Map<java.lang.String, java.lang.String>,java.util.Map<java.lang.String, ?>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)
  234. INFO : org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping - Mapped "{[/oauth/token],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.getAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException
  235. INFO : org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping - Mapped "{[/oauth/token],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException
  236. INFO : org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping - Mapped "{[/oauth/check_token],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, ?> org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint.checkToken(java.lang.String)
  237. INFO : org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping - Mapped "{[/oauth/confirm_access],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.WhitelabelApprovalEndpoint.getAccessConfirmation(java.util.Map<java.lang.String, java.lang.Object>,javax.servlet.http.HttpServletRequest) throws java.lang.Exception
  238. INFO : org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping - Mapped "{[/oauth/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.WhitelabelErrorEndpoint.handleError(javax.servlet.http.HttpServletRequest)
  239. INFO : org.springframework.security.web.DefaultSecurityFilterChain - Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/greeting/oauth/token'], Ant [pattern='/greeting/oauth/token_key'], Ant [pattern='/greeting/oauth/check_token']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@e555f9, org.springframework.security.web.context.SecurityContextPersistenceFilter@179c9de, org.springframework.security.web.header.HeaderWriterFilter@830445, org.springframework.security.web.authentication.logout.LogoutFilter@e1780f, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@171fcc8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@bdb1b1, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1c75f90, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@547ea0, org.springframework.security.web.session.SessionManagementFilter@f74cf7, org.springframework.security.web.access.ExceptionTranslationFilter@1ba730d, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@584d1e]
  240. INFO : org.springframework.security.web.DefaultSecurityFilterChain - Creating filter chain: org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration$NotOAuthRequestMatcher@1b995c6, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1191e9a, org.springframework.security.web.context.SecurityContextPersistenceFilter@ff732a, org.springframework.security.web.header.HeaderWriterFilter@6d6ad5, org.springframework.security.web.authentication.logout.LogoutFilter@41633b, org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter@55c77a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@d2289a, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@144df97, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@1b924e6, org.springframework.security.web.session.SessionManagementFilter@36f11d, org.springframework.security.web.access.ExceptionTranslationFilter@2b42c5, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@10f33b3]
  241. INFO : org.springframework.security.web.DefaultSecurityFilterChain - Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@3b8bb3, org.springframework.security.web.context.SecurityContextPersistenceFilter@60c3ae, org.springframework.security.web.header.HeaderWriterFilter@cee322, org.springframework.security.web.csrf.CsrfFilter@1584f4a, org.springframework.security.web.authentication.logout.LogoutFilter@f6d56e, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@1948232, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@8512a3, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@10515f3, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@762e46, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1d410d9, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@1a842fe, org.springframework.security.web.session.SessionManagementFilter@149de2e, org.springframework.security.web.access.ExceptionTranslationFilter@ca00c4, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@19a7f7]
  242. INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 6167 ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement