Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. ...
  2. .and()
  3. .formLogin()
  4. .successHandler(myAuthenticationSuccessHandler)
  5. .failureHandler(myAuthenticationFailureHandler)
  6. .usernameParameter(... some code ...)
  7. .passwordParameter(... some code ...)
  8. .loginProcessingUrl(... some code ...)
  9. .and()
  10. .logout()
  11. .invalidateHttpSession(true)
  12. .deleteCookies("JSESSIONID")
  13. .logoutUrl(... some code ...)
  14. .logoutSuccessHandler(noRedirectLogoutSuccessHandler)
  15. .and()
  16. .httpBasic()
  17. .and()
  18. .exceptionHandling().authenticationEntryPoint(restAuthenticationEntryPoint)
  19. .and()
  20. .addFilterBefore(new HmacAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class)
  21. ...
  22.  
  23. import java.io.IOException;
  24. import java.util.Arrays;
  25.  
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28.  
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.security.authentication.AuthenticationManager;
  32. import org.springframework.security.authentication.InternalAuthenticationServiceException;
  33. import org.springframework.security.core.Authentication;
  34. import org.springframework.security.core.AuthenticationException;
  35. import org.springframework.security.core.context.SecurityContextHolder;
  36. import org.springframework.security.core.session.SessionRegistry;
  37. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  38. import org.springframework.security.web.authentication.session.ChangeSessionIdAuthenticationStrategy;
  39. import org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy;
  40. import org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy;
  41. import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
  42. import org.springframework.security.web.util.matcher.RequestMatcher;
  43.  
  44. import mx.i4b.sisintadmin.sec.WebSecurityConfig.RestWebSecurityConfigurationAdapter;
  45. import mx.i4b.sisintadmin.service.CryptoService;
  46. import mx.i4b.sisintadmin.util.EntityHelper;
  47. import mx.i4b.sisintadmin.util.Util;
  48.  
  49. public class HmacAuthorizationFilter extends UsernamePasswordAuthenticationFilter /*AbstractAuthenticationProcessingFilter*/ {
  50.  
  51. @SuppressWarnings("unused")
  52. private final Logger logger = LoggerFactory.getLogger(getClass());
  53.  
  54. public HmacAuthorizationFilter() {
  55. super();
  56. setRequiresAuthenticationRequestMatcher(authMatcher());
  57. }
  58.  
  59. protected RequestMatcher authMatcher() {
  60. return new RequestMatcher() {
  61. @Override
  62. public boolean matches(HttpServletRequest request) {
  63. boolean result = true;
  64. result = result && Util.isDiferenteVacio(request.getHeader("Authorization"));
  65. result = result && Util.isDiferenteVacio(request.getHeader("Date"));
  66. return result;
  67. }
  68. };
  69. }
  70.  
  71. @Override
  72. public Authentication attemptAuthentication(HttpServletRequest request,
  73. HttpServletResponse response)
  74. throws AuthenticationException {
  75.  
  76. // Get authorization header
  77. String credentials = request.getHeader("Authorization");
  78.  
  79. // get timestamp
  80. String timestamp = request.getHeader("Date");
  81.  
  82. // If there's not credentials or date, return...
  83. if ((credentials == null) || (timestamp == null)) {
  84. return null;
  85. }
  86.  
  87. ----- some code ----
  88.  
  89. HmacAuthorizationToken token = cryptoService.createHmacAutorizationToken(... some code ...);
  90.  
  91. token.setDetails(authenticationDetailsSource.buildDetails(request));
  92.  
  93. //FIXME FIND OUT WHY this.getAuthenticationManager() IS NULL USING @Autowired
  94. AuthenticationManager authenticationManager = EntityHelper.getAuthenticationManager();
  95.  
  96. Authentication result = authenticationManager.authenticate(token);
  97.  
  98.  
  99.  
  100. ////////////////////////////////////////////////////////////////////
  101. //// Session configuration /////
  102. ////////////////////////////////////////////////////////////////////
  103.  
  104. SessionRegistry sessionRegistry = EntityHelper.getSessionRegistry();
  105.  
  106. ConcurrentSessionControlAuthenticationStrategy cscas =
  107. new ConcurrentSessionControlAuthenticationStrategy(sessionRegistry);
  108.  
  109. ChangeSessionIdAuthenticationStrategy csias = new ChangeSessionIdAuthenticationStrategy();
  110.  
  111. RegisterSessionAuthenticationStrategy rsas =
  112. new RegisterSessionAuthenticationStrategy(sessionRegistry);
  113.  
  114. CompositeSessionAuthenticationStrategy sessionAuthenticationStrategy =
  115. new CompositeSessionAuthenticationStrategy(Arrays.asList(cscas, csias, rsas));
  116.  
  117. sessionAuthenticationStrategy.onAuthentication(result, request, response);
  118.  
  119.  
  120. SecurityContextHolder.getContext().setAuthentication(result);
  121.  
  122.  
  123. this.setAuthenticationSuccessHandler(
  124. RestWebSecurityConfigurationAdapter.myAuthenticationSuccessHandler);
  125.  
  126. this.setAuthenticationFailureHandler(
  127. RestWebSecurityConfigurationAdapter.myAuthenticationFailureHandler);
  128.  
  129. //this.setContinueChainBeforeSuccessfulAuthentication(true);
  130.  
  131. return result;
  132. }
  133.  
  134.  
  135. }
  136.  
  137. public static AuthenticationManager getAuthenticationManager() {
  138.  
  139. AuthenticationManager authenticationManager =
  140. getContext().getBean(AuthenticationManager.class);
  141.  
  142. if (authenticationManager == null) {
  143. final Logger logger = LoggerFactory.getLogger(EntityHelper.class);
  144. logger.error("**************************************** EN EntityHelper RECEIVED AuthenticationManager NULL ****************************************");
  145. }
  146.  
  147. return authenticationManager;
  148. }
  149.  
  150. 2016-06-21 18:48:53,427 DEBUG [http-nio-8084-exec-3] (ProviderManager.java:152) - Authentication attempt using some.package.sec.HmacAuthenticationProvider
  151. 2016-06-21 18:48:53,427 DEBUG [http-nio-8084-exec-3] (HmacAuthenticationProvider.java:101) - retrieveUser HMAC . LOGIN username: ---- SOME CODE ---- desde la IP: 0:0:0:0:0:0:0:1
  152. 2016-06-21 18:48:53,691 INFO [http-nio-8084-exec-3] (UsuarioServiceImpl.java:1398) - LOGIN username: ---- SOME CODE ---- desde la IP: 0:0:0:0:0:0:0:1, Exitoso
  153. 2016-06-21 18:48:53,780 INFO [http-nio-8084-exec-3] (AuditListener.java:42) - AuditEvent [timestamp=Tue Jun 21 18:48:53 CDT 2016, principal=---- SOME CODE ----, type=AUTHENTICATION_SUCCESS, data={details=org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null}]
  154. 2016-06-21 18:48:53,785 DEBUG [http-nio-8084-exec-3] (CompositeSessionAuthenticationStrategy.java:81) - Delegating to org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy@51652639
  155. 2016-06-21 18:48:53,786 DEBUG [http-nio-8084-exec-3] (CompositeSessionAuthenticationStrategy.java:81) - Delegating to org.springframework.security.web.authentication.session.ChangeSessionIdAuthenticationStrategy@47b091ee
  156. 2016-06-21 18:48:53,786 DEBUG [http-nio-8084-exec-3] (CompositeSessionAuthenticationStrategy.java:81) - Delegating to org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy@193cc507
  157. 2016-06-21 18:48:53,787 DEBUG [http-nio-8084-exec-3] (HttpSessionEventPublisher.java:66) - Publishing event: org.springframework.security.web.session.HttpSessionCreatedEvent[source=org.apache.catalina.session.StandardSessionFacade@64ab5b82]
  158. 2016-06-21 18:48:53,788 DEBUG [http-nio-8084-exec-3] (SessionRegistryImpl.java:107) - Registering session 8A4D0A8AF99E7F74D6CFCD3DDAC86522, for principal some.package.sec.AppUserDetails@4476b273: Username: ---- SOME CODE ----; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_MAIN_USER
  159. 2016-06-21 18:48:53,788 DEBUG [http-nio-8084-exec-3] (AbstractAuthenticationProcessingFilter.java:319) - Authentication success. Updating SecurityContextHolder to contain: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@2588dcdb: Principal: some.package.sec.AppUserDetails@4476b273: Username: ---- SOME CODE ----; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_MAIN_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_MAIN_USER
  160. 2016-06-21 18:48:53,789 DEBUG [http-nio-8084-exec-3] (HttpSessionSecurityContextRepository.java:327) - SecurityContext stored to HttpSession: 'org.springframework.security.core.context.SecurityContextImpl@2588dcdb: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@2588dcdb: Principal: some.package.sec.AppUserDetails@4476b273: Username: ---- SOME CODE ----; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_MAIN_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_MAIN_USER'
  161. 2016-06-21 18:48:53,790 DEBUG [http-nio-8084-exec-3] (SecurityContextPersistenceFilter.java:97) - SecurityContextHolder now cleared, as request processing completed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement