Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.96 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Username Password Authentication in Spring Security
  2. <beans:bean id="myProvider" class="com.example.MyProvider"></beans:bean>
  3.  
  4. <authentication-manager>
  5.     <authentication-provider ref="myProvider"></authentication-provider>
  6. </authentication-manager>
  7.        
  8. public UserDetails loadUserByUsername(String username)
  9.        
  10. public Collection<GrantedAuthority> getAuthorities()
  11.        
  12. <http auto-config="true">
  13.     <intercept-url pattern="/admin/**" access="IS_AUTHENTICATED_REMEMBERED"/>
  14.     <intercept-url pattern="/welcome/**" access="IS_AUTHENTICATED_REMEMBERED" />
  15.     <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
  16.     <form-login login-page="/login" />
  17.     <logout logout-success-url="/" logout-url="/logout" />
  18.     <!-- Limits the number of concurent sessions a user can have
  19.     <concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="true"/>
  20. -->
  21. </http>
  22.  
  23.  
  24. <!--
  25. Usernames/Passwords are
  26.     rod/koala
  27.     dianne/emu
  28.     scott/wombat
  29. -->
  30.  
  31. <authentication-manager>
  32.     <authentication-provider>
  33.         <password-encoder hash="md5"/>
  34.         <user-service>
  35.             <user name="rod" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
  36.             <user name="dianne" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" />
  37.             <user name="scott" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" />
  38.         </user-service>
  39.     </authentication-provider>
  40. </authentication-manager>
  41.        
  42. <filter>
  43.     <filter-name>springSecurityFilterChain</filter-name>
  44.     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  45. </filter>
  46.  
  47. <filter-mapping>
  48.   <filter-name>springSecurityFilterChain</filter-name>
  49.   <url-pattern>/*</url-pattern>
  50. </filter-mapping>
  51. <listener>
  52.     <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
  53. </listener>
  54.        
  55. <beans:bean id="authenticationManager"
  56.      class="org.springframework.security.authentication.ProviderManager">
  57.  
  58.   <beans:property name="providers">
  59.     <beans:list>
  60.       <beans:ref local="myAuthenticationProvider"/>
  61.     </beans:list>
  62.   </beans:property>
  63. </beans:bean>
  64.  
  65. <beans:bean id="myAuthenticationProvider" class="com.example.MyAuthenticationProvider">
  66. </beans:bean>
  67.  
  68. <authentication-manager>
  69.     <authentication-provider ref="myAuthenticationProvider"/>
  70. </authentication-manager>
  71.        
  72. public class AConnexAuthenticationProvider implements AuthenticationProvider {
  73.  
  74.     static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
  75.  
  76.     static {
  77.       AUTHORITIES.add(new GrantedAuthorityImpl("ROLE_USER"));
  78.     }
  79.  
  80.     @Override
  81.     public Authentication authenticate(Authentication auth)
  82.             throws AuthenticationException {
  83.         return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(), AUTHORITIES);
  84.     }
  85.  
  86.     @Override
  87.     public boolean supports(Class<? extends Object> paramClass) {
  88.         return true;
  89.     }
  90. }