1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.richclient.security;
17
18 import org.springframework.security.Authentication;
19 import org.springframework.security.AuthenticationException;
20 import org.springframework.security.AuthenticationManager;
21 import org.springframework.security.BadCredentialsException;
22 import org.springframework.security.GrantedAuthority;
23 import org.springframework.security.GrantedAuthorityImpl;
24 import org.springframework.security.LockedException;
25 import org.springframework.security.providers.TestingAuthenticationToken;
26
27
28
29
30
31
32 public class TestAuthenticationManager implements AuthenticationManager {
33
34
35 public static final String ROLE_EXPECTED = "ROLE_EXPECTED";
36
37
38 public static final Authentication VALID_USER1 = new TestingAuthenticationToken( "USER1", "FOO",
39 new GrantedAuthority[] { new GrantedAuthorityImpl( ROLE_EXPECTED ) } );
40
41
42 public static final Authentication VALID_USER2 = new TestingAuthenticationToken( "USER2", "FOO");
43
44
45 public static final Authentication BAD_CREDENTIALS = new TestingAuthenticationToken( "FAIL", "FOO");
46
47
48 public static final Authentication LOCKED = new TestingAuthenticationToken( "LOCKED", "FOO");
49
50
51
52
53 public static Authentication makeAuthentication( String user, String password, String role ) {
54 return new TestingAuthenticationToken( user, password,
55 new GrantedAuthority[] { new GrantedAuthorityImpl( role ) } );
56 }
57
58
59
60
61 public Authentication authenticate(Authentication authentication) throws AuthenticationException {
62 if( authentication == BAD_CREDENTIALS ) {
63 throw new BadCredentialsException( "Bad credentials" );
64 } else if( authentication == LOCKED ) {
65 throw new LockedException( "Account is locked" );
66 }
67 return authentication;
68 }
69 }