Advertisement
Guest User

Untitled

a guest
May 9th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. @Configuration
  2. @Component
  3. public class OAuth2ServerConfiguration {
  4.  
  5. private static final String RESOURCE_ID = "restservice";
  6.  
  7. @Configuration
  8. @Component
  9. @EnableResourceServer
  10. @Order(3)
  11. protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  12. @Override
  13. public void configure(ResourceServerSecurityConfigurer resources) {
  14. resources.resourceId(RESOURCE_ID);
  15. }
  16.  
  17. @Override
  18. public void configure(HttpSecurity http) throws Exception {
  19. http.authorizeRequests().antMatchers("/users").hasRole("ADMIN").antMatchers("/greeting").authenticated();
  20. }
  21. }
  22.  
  23. @Configuration
  24. @Component
  25. @EnableAuthorizationServer
  26. protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  27. private TokenStore tokenStore = new InMemoryTokenStore();
  28.  
  29. @Autowired
  30. @Qualifier("authenticationManagerBean")
  31. private AuthenticationManager authenticationManager;
  32.  
  33. @Autowired
  34. private CustomUserDetailsService userDetailsService;
  35.  
  36. @Override
  37. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  38. endpoints.tokenStore(this.tokenStore).authenticationManager(this.authenticationManager)
  39. .userDetailsService(userDetailsService);
  40. }
  41.  
  42. @Override
  43. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  44. clients.inMemory().withClient("clientapp").authorizedGrantTypes("password", "refresh_token")
  45. .authorities("USER").scopes("read", "write").resourceIds(RESOURCE_ID).secret("123456");
  46. }
  47.  
  48. @Bean
  49. @Primary
  50. public DefaultTokenServices tokenServices() {
  51. DefaultTokenServices tokenServices = new DefaultTokenServices();
  52. tokenServices.setSupportRefreshToken(true);
  53. tokenServices.setTokenStore(this.tokenStore);
  54. return tokenServices;
  55. }
  56. }
  57. }
  58.  
  59. <?xml version="1.0" encoding="UTF-8"?>
  60. <beans xmlns="http://www.springframework.org/schema/beans"
  61. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  62. xmlns:context="http://www.springframework.org/schema/context"
  63. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  64. xmlns:oauth2="http://www.springframework.org/schema/security/oauth2"
  65. xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
  66. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  67. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  68. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd">
  69.  
  70. <context:annotation-config base-package="net.mmm.ma"></context:annotation-config>
  71. <context:component-scan base-package="net.mmm.ma" />
  72. <jpa:repositories base-package="net.mmm.ma"></jpa:repositories>
  73.  
  74.  
  75. <bean id="dataSource"
  76. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  77. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  78. <property name="url" value="jdbc:mysql://localhost:3306/test20"></property>
  79. <property name="username" value="root"></property>
  80. <property name="password" value=""></property>
  81. </bean>
  82. <bean id="persistenceUnitManager"
  83. class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
  84. <property name="persistenceXmlLocations">
  85. <list>
  86. <value>classpath*:META-INF/persistence.xml</value>
  87. </list>
  88. </property>
  89. <property name="defaultDataSource" ref="dataSource"></property>
  90. </bean>
  91. <bean id="entityManagerFactory"
  92. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  93. <property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
  94. <property name="persistenceUnitName" value="MY_P"></property>
  95. </bean>
  96. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  97. <property name="entityManagerFactory" ref="entityManagerFactory"></property>
  98. </bean>
  99. </beans>
  100.  
  101. <?xml version="1.0" encoding="UTF-8"?>
  102. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
  103. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  105.  
  106. <!-- The definition of the Root Spring Container shared by all Servlets
  107. and Filters -->
  108. <context-param>
  109. <param-name>contextConfigLocation</param-name>
  110. <param-value>classpath*:applicationContext.xml</param-value>
  111. </context-param>
  112.  
  113. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  114. <listener>
  115. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  116. </listener>
  117.  
  118. <!-- Processes application requests -->
  119. <servlet>
  120. <servlet-name>appServlet</servlet-name>
  121. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  122. <init-param>
  123. <param-name>contextConfigLocation</param-name>
  124. <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  125. </init-param>
  126. <load-on-startup>1</load-on-startup>
  127. </servlet>
  128.  
  129. <servlet-mapping>
  130. <servlet-name>appServlet</servlet-name>
  131. <url-pattern>/*</url-pattern>
  132. </servlet-mapping>
  133. </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement