Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.11 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:oauth="http://www.springframework.org/schema/security/oauth2"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:sec="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  8. http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd ">
  11.  
  12.  
  13. <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="authenticationManager"
  14. xmlns="http://www.springframework.org/schema/security" >
  15.  
  16. <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" method="POST" />
  17. <anonymous enabled="false" />
  18. <http-basic entry-point-ref="clientAuthenticationEntryPoint" />
  19. <custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
  20. <access-denied-handler ref="oauthAccessDeniedHandler" />
  21. </http>
  22.  
  23. <http pattern="/protected/**" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint"
  24. xmlns="http://www.springframework.org/schema/security">
  25. <anonymous enabled="false" />
  26. <intercept-url pattern="/protected/**" method="GET" access="ROLE_APP" />
  27. <!-- <intercept-url pattern="/resources/**" access="IS_AUTHENTICATED_FULLY" /> -->
  28. <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
  29. <access-denied-handler ref="oauthAccessDeniedHandler" />
  30. </http>
  31.  
  32. <http pattern="/logout" create-session="never"
  33. entry-point-ref="oauthAuthenticationEntryPoint"
  34. xmlns="http://www.springframework.org/schema/security">
  35. <anonymous enabled="false" />
  36. <intercept-url pattern="/logout" method="GET" />
  37. <sec:logout invalidate-session="true" logout-url="/logout" success-handler-ref="logoutSuccessHandler" />
  38. <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
  39. <access-denied-handler ref="oauthAccessDeniedHandler" />
  40. </http>
  41.  
  42. <bean id="logoutSuccessHandler" class="com.example.myproject.security.LogoutImpl" >
  43. <property name="tokenstore" ref="tokenStore"></property>
  44. </bean>
  45.  
  46. <bean id="oauthAuthenticationEntryPoint"
  47. class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
  48. <property name="exceptionTranslator" ref="myExceptionTranslator"></property>
  49. </bean>
  50.  
  51. <bean id="myExceptionTranslator"
  52. class="org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator">
  53. </bean>
  54.  
  55. <bean id="clientAuthenticationEntryPoint"
  56. class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
  57. <property name="realmName" value="springsec/client" />
  58. <property name="typeName" value="Basic" />
  59. </bean>
  60.  
  61. <bean id="oauthAccessDeniedHandler"
  62. class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler">
  63. </bean>
  64.  
  65. <bean id="clientCredentialsTokenEndpointFilter"
  66. class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
  67. <property name="authenticationManager" ref="authenticationManager" />
  68. </bean>
  69.  
  70. <authentication-manager alias="authenticationManager"
  71. xmlns="http://www.springframework.org/schema/security">
  72. <authentication-provider user-service-ref="clientDetailsUserService" />
  73. </authentication-manager>
  74.  
  75. <bean id="clientDetailsUserService"
  76. class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
  77. <constructor-arg ref="clientDetails" />
  78. </bean>
  79.  
  80. <bean id="clientDetails" class="com.example.myproject.service.ClientService"/>
  81.  
  82. <authentication-manager id="userAuthenticationManager"
  83. xmlns="http://www.springframework.org/schema/security">
  84. <authentication-provider user-service-ref="userService">
  85. </authentication-provider>
  86. </authentication-manager>
  87.  
  88. <bean id="userService"
  89. class="com.example.myproject.service.UserService">
  90. </bean>
  91.  
  92. <oauth:authorization-server
  93. client-details-service-ref="clientDetails" token-services-ref="tokenServices">
  94. <oauth:authorization-code />
  95. <oauth:implicit/>
  96. <oauth:refresh-token/>
  97. <oauth:client-credentials />
  98. <oauth:password authentication-manager-ref="userAuthenticationManager"/>
  99. </oauth:authorization-server>
  100.  
  101. <oauth:resource-server id="resourceServerFilter"
  102. resource-id="springsec" token-services-ref="tokenServices" />
  103.  
  104. <!-- <bean id="tokenStore"
  105. class="org.springframework.security.oauth2.provider.token.InMemoryTokenStore" /> -->
  106.  
  107. <bean id="tokenStore"
  108. class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore" >
  109. <property name="authenticationKeyGenerator">
  110. <bean class="com.example.myproject.service.UniqueAuthenticationKeyGenerator" />
  111. </property>
  112. </bean>
  113.  
  114. <bean id="tokenServices"
  115. class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
  116. <property name="tokenStore" ref="tokenStore" />
  117. <property name="supportRefreshToken" value="true" />
  118. <property name="accessTokenValiditySeconds" value="300000"></property>
  119. <property name="clientDetailsService" ref="clientDetails" />
  120. <property name="tokenEnhancer"><bean class="com.example.myproject.service.CustomTokenEnhancer" /></property>
  121. </bean>
  122.  
  123. <sec:global-method-security
  124. pre-post-annotations="enabled" proxy-target-class="true">
  125. <!--you could also wire in the expression handler up at the layer of the
  126. http filters. See https://jira.springsource.org/browse/SEC-1452 -->
  127. <sec:expression-handler ref="oauthExpressionHandler" />
  128. </sec:global-method-security>
  129.  
  130. <oauth:expression-handler id="oauthExpressionHandler" />
  131. <oauth:web-expression-handler id="oauthWebExpressionHandler" />
  132.  
  133. </beans>
  134.  
  135. @Component
  136. public class ClientService implements ClientDetailsService {
  137.  
  138. @Autowired
  139. private OauthRepository oauthRepository;
  140.  
  141. @Override
  142. public ClientDetails loadClientByClientId(String s) throws ClientRegistrationException{
  143. BaseClientDetails clientDetails = oauthRepository.getByClientId(s);
  144. return clientDetails;
  145.  
  146. }
  147. }
  148.  
  149. @Component
  150. public class UserService implements UserDetailsService {
  151.  
  152. @Autowired
  153. private OauthRepository oauthRepository;
  154.  
  155. @Override
  156. public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
  157. UserDetails user = oauthRepository.getByUsername(s);
  158. return user;
  159.  
  160. }
  161. }
  162.  
  163. @Repository
  164. @Transactional
  165. public class OauthRepository {
  166.  
  167. @Autowired
  168. private SessionFactory sessionFactory;
  169.  
  170. @Autowired
  171. private InMemoryTokenStore tokenStore;
  172.  
  173. private org.hibernate.Session getCurrentSession(){
  174. return sessionFactory.getCurrentSession();
  175. }
  176.  
  177.  
  178.  
  179. public UserDetails getByUsername(String username) {
  180.  
  181. MyUser user=new MyUser();
  182. user.setUserName(username);
  183. Query query=getCurrentSession().createQuery("FROM User WHERE userName=:usrName");
  184. query.setParameter("usrName", username);
  185. List<SiUser> getUser=query.list();
  186.  
  187. User act=getUser.get(0);
  188. user.setPassword(act.getPassword());
  189. user.setUserId(act.getUserId());
  190. user.setAuthorities(getAuthorities(act.getUserId()));
  191.  
  192. return user;
  193. }
  194.  
  195. public BaseClientDetails getByClientId(String clientId) {
  196. System.out.println(" *** OauthRepository.getByClientId "+clientId);
  197.  
  198. Query query=getCurrentSession().createQuery("FROM OauthClientDetails WHERE clientId=:clientId");
  199. query.setParameter("clientId", clientId);
  200. List<OauthClientDetails> getClient=query.list();
  201.  
  202. OauthClientDetails oauthClient=getClient.get(0);
  203. BaseClientDetails details = new BaseClientDetails();
  204. details.setClientId(oauthClient.getClientId());
  205. List<String> grantTypesList = Arrays.asList(oauthClient.getAuthorizedGrantTypes().split(","));
  206. details.setAuthorizedGrantTypes(grantTypesList);
  207. details.setClientSecret(oauthClient.getClientSecret());
  208.  
  209. return details;
  210.  
  211.  
  212. }
  213.  
  214. /**
  215. * Retrieves a collection of {@link GrantedAuthority} based on a numerical role
  216. * @param role the numerical role
  217. * @return a collection of {@link GrantedAuthority
  218. */
  219. public Collection<GrantedAuthority> getAuthorities(Integer role) {
  220. List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
  221. return authList;
  222. }
  223. /**
  224. * Converts a numerical role to an equivalent list of roles
  225. * @param role the numerical role
  226. * @return list of roles as as a list of {@link String}
  227. */
  228. public List<String> getRoles(Integer role) {
  229. List<String> roles = new ArrayList<String>();
  230.  
  231. Query query=getCurrentSession().createQuery("FROM UserRole WHERE userID=:userId");
  232. query.setParameter("userId", role);
  233. List<SiUserRole> getUser=query.list();
  234.  
  235. UserRole actRole=getUser.get(0);
  236. roles.add(actRole.getRole());
  237. return roles;
  238. }
  239. /**
  240. * Wraps {@link String} roles to {@link SimpleGrantedAuthority} objects
  241. * @param roles {@link String} of roles
  242. * @return list of granted authorities
  243. */
  244. public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {
  245. List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
  246. for (String role : roles) {
  247. authorities.add(new GrantedAuthorityImpl(role));
  248. }
  249. return authorities;
  250. }
  251.  
  252.  
  253. }
  254.  
  255. <?xml version="1.0" encoding="UTF-8"?>
  256. <beans xmlns="http://www.springframework.org/schema/beans"
  257. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  258. xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
  259. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  260. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  261. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
  262. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
  263. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  264. <!-- @author Nagesh.Chauhan(neel4soft@gmail.com) -->
  265. <context:annotation-config />
  266. <context:component-scan base-package="com.example.myproject" />
  267. <mvc:annotation-driven />
  268.  
  269. <bean id="multipartResolver"
  270. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  271.  
  272. <!-- one of the properties available; the maximum file size in bytes -->
  273. <property name="maxUploadSize" value="1000000000" />
  274. </bean>
  275.  
  276. <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  277. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  278. <property name="url" value="jdbc:mysql://localhost:3306/MyDatabase"/>
  279. <property name="username" value="username"/>
  280. <property name="password" value="password"/>
  281. <property name="validationQuery" value="SELECT 1"/>
  282. </bean>
  283.  
  284. <!-- Hibernate Session Factory -->
  285. <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  286. <property name="dataSource" ref="myDataSource"/>
  287. <property name="packagesToScan">
  288. <array>
  289. <value>com.example.myproject</value>
  290. </array>
  291. </property>
  292. <property name="hibernateProperties">
  293. <value>
  294. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  295. </value>
  296. </property>
  297. </bean>
  298. <!-- Hibernate Transaction Manager -->
  299. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  300. <property name="sessionFactory" ref="mySessionFactory"/>
  301. </bean>
  302.  
  303. <!-- Activates annotation based transaction management -->
  304. <tx:annotation-driven transaction-manager="transactionManager"/>
  305. </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement