SEC-73: Support storage and retrieval of actual Principal object (such as UserDetails) from PrnicipalAcegiUserToken.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,16 +31,18 @@ public class PrincipalAcegiUserToken extends AbstractAdapterAuthenticationToken
|
||||
implements Principal {
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private Object principal;
|
||||
private String password;
|
||||
private String username;
|
||||
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public PrincipalAcegiUserToken(String key, String username,
|
||||
String password, GrantedAuthority[] authorities) {
|
||||
String password, GrantedAuthority[] authorities, Object principal) {
|
||||
super(key, authorities);
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.principal = principal;
|
||||
}
|
||||
|
||||
protected PrincipalAcegiUserToken() {
|
||||
@@ -58,6 +60,10 @@ public class PrincipalAcegiUserToken extends AbstractAdapterAuthenticationToken
|
||||
}
|
||||
|
||||
public Object getPrincipal() {
|
||||
return this.username;
|
||||
if (this.principal == null) {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
return this.principal;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,7 @@ import org.acegisecurity.Authentication;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
|
||||
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
|
||||
@@ -59,7 +60,7 @@ public class AuthByAdapterTests extends TestCase {
|
||||
PrincipalAcegiUserToken token = new PrincipalAcegiUserToken("my_password",
|
||||
"Test", "Password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
"ROLE_TWO")}, null);
|
||||
assertTrue(provider.supports(token.getClass()));
|
||||
|
||||
Authentication response = provider.authenticate(token);
|
||||
@@ -122,7 +123,7 @@ public class AuthByAdapterTests extends TestCase {
|
||||
|
||||
// Should fail as PrincipalAcegiUserToken has different key
|
||||
PrincipalAcegiUserToken token = new PrincipalAcegiUserToken("wrong_password",
|
||||
"Test", "Password", null);
|
||||
"Test", "Password", null, null);
|
||||
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
|
||||
+4
-1
@@ -19,7 +19,9 @@ import junit.framework.TestCase;
|
||||
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
|
||||
import org.acegisecurity.context.SecurityContextHolder;
|
||||
|
||||
import org.acegisecurity.util.MockFilterChain;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -53,7 +55,8 @@ public class HttpRequestIntegrationFilterTests extends TestCase {
|
||||
HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")},
|
||||
null);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setUserPrincipal(principal);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -52,7 +52,7 @@ public class PrincipalAcegiUserTokenTests extends TestCase {
|
||||
PrincipalAcegiUserToken token = new PrincipalAcegiUserToken("my_password",
|
||||
"Test", "Password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
"ROLE_TWO")}, null);
|
||||
assertEquals("Test", token.getPrincipal());
|
||||
assertEquals("Password", token.getCredentials());
|
||||
assertEquals("my_password".hashCode(), token.getKeyHash());
|
||||
|
||||
+19
-13
@@ -12,6 +12,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.acegisecurity.context;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
@@ -20,6 +21,7 @@ import org.acegisecurity.Authentication;
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
import org.acegisecurity.MockFilterConfig;
|
||||
|
||||
import org.acegisecurity.adapters.PrincipalAcegiUserToken;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -42,6 +44,8 @@ import javax.servlet.ServletResponse;
|
||||
* @version $Id$
|
||||
*/
|
||||
public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public HttpSessionContextIntegrationFilterTests() {
|
||||
super();
|
||||
}
|
||||
@@ -50,6 +54,8 @@ public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(HttpSessionContextIntegrationFilterTests.class);
|
||||
}
|
||||
@@ -80,7 +86,8 @@ public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
// Build an Authentication object we simulate came from HttpSession
|
||||
PrincipalAcegiUserToken sessionPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] { new GrantedAuthorityImpl("SOME_ROLE") });
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")},
|
||||
null);
|
||||
|
||||
// Build a Context to store in HttpSession (simulating prior request)
|
||||
SecurityContext sc = new SecurityContextImpl();
|
||||
@@ -120,14 +127,14 @@ public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
// Build an Authentication object we simulate came from HttpSession
|
||||
PrincipalAcegiUserToken sessionPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] { new GrantedAuthorityImpl("SOME_ROLE") });
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")},
|
||||
null);
|
||||
|
||||
// Build an Authentication object we simulate our Authentication changed it to
|
||||
PrincipalAcegiUserToken updatedPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {
|
||||
new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")
|
||||
});
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")},
|
||||
null);
|
||||
|
||||
// Build a Context to store in HttpSession (simulating prior request)
|
||||
SecurityContext sc = new SecurityContextImpl();
|
||||
@@ -163,9 +170,8 @@ public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
// Build an Authentication object we simulate our Authentication changed it to
|
||||
PrincipalAcegiUserToken updatedPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {
|
||||
new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")
|
||||
});
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")},
|
||||
null);
|
||||
|
||||
// Build a mock request
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -213,9 +219,8 @@ public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
// Build an Authentication object we simulate our Authentication changed it to
|
||||
PrincipalAcegiUserToken updatedPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {
|
||||
new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")
|
||||
});
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")},
|
||||
null);
|
||||
|
||||
// Build a mock request
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -249,6 +254,8 @@ public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
filter.destroy();
|
||||
}
|
||||
|
||||
//~ Inner Classes ==========================================================
|
||||
|
||||
private class MockFilterChain extends TestCase implements FilterChain {
|
||||
private Authentication changeContextHolder;
|
||||
private Authentication expectedOnContextHolder;
|
||||
@@ -261,8 +268,7 @@ public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
this.toThrowDuringChain = toThrowDuringChain;
|
||||
}
|
||||
|
||||
private MockFilterChain() {
|
||||
}
|
||||
private MockFilterChain() {}
|
||||
|
||||
public void doFilter(ServletRequest arg0, ServletResponse arg1)
|
||||
throws IOException, ServletException {
|
||||
|
||||
Reference in New Issue
Block a user