SEC-1012: Refactoring of use of GrantedAuthority[] to generified collections
This commit is contained in:
+11
-12
@@ -1,6 +1,8 @@
|
||||
package org.springframework.security.ui.portlet;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.portlet.PortletRequest;
|
||||
|
||||
@@ -9,25 +11,22 @@ import org.springframework.security.MutableGrantedAuthoritiesContainer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class PortletPreAuthenticatedAuthenticationDetails extends PortletAuthenticationDetails implements MutableGrantedAuthoritiesContainer {
|
||||
|
||||
private GrantedAuthority[] preAuthenticatedGrantedAuthorities = null;
|
||||
|
||||
|
||||
private List<GrantedAuthority> preAuthenticatedGrantedAuthorities = null;
|
||||
|
||||
public PortletPreAuthenticatedAuthenticationDetails(PortletRequest request) {
|
||||
super(request);
|
||||
}
|
||||
|
||||
public GrantedAuthority[] getGrantedAuthorities() {
|
||||
|
||||
public List<GrantedAuthority> getGrantedAuthorities() {
|
||||
Assert.notNull(preAuthenticatedGrantedAuthorities, "Pre-authenticated granted authorities have not been set");
|
||||
GrantedAuthority[] result = new GrantedAuthority[preAuthenticatedGrantedAuthorities.length];
|
||||
System.arraycopy(preAuthenticatedGrantedAuthorities, 0, result, 0, result.length);
|
||||
return result;
|
||||
return preAuthenticatedGrantedAuthorities;
|
||||
}
|
||||
|
||||
public void setGrantedAuthorities(GrantedAuthority[] authorities) {
|
||||
this.preAuthenticatedGrantedAuthorities = new GrantedAuthority[authorities.length];
|
||||
System.arraycopy(authorities, 0, preAuthenticatedGrantedAuthorities, 0, preAuthenticatedGrantedAuthorities.length);
|
||||
public void setGrantedAuthorities(List<GrantedAuthority> authorities) {
|
||||
this.preAuthenticatedGrantedAuthorities = Collections.unmodifiableList(authorities);
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(super.toString() + "; ");
|
||||
|
||||
+6
-4
@@ -1,18 +1,19 @@
|
||||
package org.springframework.security.ui.portlet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.portlet.PortletRequest;
|
||||
|
||||
import org.springframework.security.ui.preauth.j2ee.AbstractPreAuthenticatedAuthenticationDetailsSource;
|
||||
|
||||
public class PortletPreAuthenticatedAuthenticationDetailsSource extends AbstractPreAuthenticatedAuthenticationDetailsSource {
|
||||
|
||||
|
||||
public PortletPreAuthenticatedAuthenticationDetailsSource() {
|
||||
setClazz(PortletPreAuthenticatedAuthenticationDetails.class);
|
||||
}
|
||||
|
||||
protected String[] getUserRoles(Object context, String[] mappableRoles) {
|
||||
protected Collection<String> getUserRoles(Object context, String[] mappableRoles) {
|
||||
ArrayList portletRoles = new ArrayList();
|
||||
|
||||
for (int i = 0; i < mappableRoles.length; i++) {
|
||||
@@ -20,8 +21,9 @@ public class PortletPreAuthenticatedAuthenticationDetailsSource extends Abstract
|
||||
portletRoles.add(mappableRoles[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return (String[]) portletRoles.toArray(new String[portletRoles.size()]);
|
||||
portletRoles.trimToSize();
|
||||
|
||||
return portletRoles;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+44
-44
@@ -40,73 +40,73 @@ import org.springframework.mock.web.portlet.MockRenderResponse;
|
||||
*/
|
||||
public class PortletTestUtils {
|
||||
|
||||
//~ Static fields/initializers =====================================================================================
|
||||
//~ Static fields/initializers =====================================================================================
|
||||
|
||||
public static final String PORTALROLE1 = "ONE";
|
||||
public static final String PORTALROLE2 = "TWO";
|
||||
public static final String PORTALROLE1 = "ONE";
|
||||
public static final String PORTALROLE2 = "TWO";
|
||||
|
||||
public static final String TESTUSER = "testuser";
|
||||
public static final String TESTCRED = PortletRequest.FORM_AUTH;
|
||||
public static final String TESTROLE1 = "ROLE_" + PORTALROLE1;
|
||||
public static final String TESTROLE2 = "ROLE_" + PORTALROLE2;
|
||||
public static final String TESTUSER = "testuser";
|
||||
public static final String TESTCRED = PortletRequest.FORM_AUTH;
|
||||
public static final String TESTROLE1 = "ROLE_" + PORTALROLE1;
|
||||
public static final String TESTROLE2 = "ROLE_" + PORTALROLE2;
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public static UserDetails createUser() {
|
||||
return new User(PortletTestUtils.TESTUSER, "dummy", true, true, true, true,
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl(TESTROLE1), new GrantedAuthorityImpl(TESTROLE2)});
|
||||
}
|
||||
public static UserDetails createUser() {
|
||||
return new User(PortletTestUtils.TESTUSER, "dummy", true, true, true, true,
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl(TESTROLE1), new GrantedAuthorityImpl(TESTROLE2)});
|
||||
}
|
||||
|
||||
public static void applyPortletRequestSecurity(MockPortletRequest request) {
|
||||
request.setRemoteUser(TESTUSER);
|
||||
request.setUserPrincipal(new TestingAuthenticationToken(TESTUSER, TESTCRED));
|
||||
request.addUserRole(PORTALROLE1);
|
||||
request.addUserRole(PORTALROLE2);
|
||||
request.setRemoteUser(TESTUSER);
|
||||
request.setUserPrincipal(new TestingAuthenticationToken(TESTUSER, TESTCRED));
|
||||
request.addUserRole(PORTALROLE1);
|
||||
request.addUserRole(PORTALROLE2);
|
||||
// request.setAuthType(PortletRequest.FORM_AUTH);
|
||||
}
|
||||
|
||||
public static MockRenderRequest createRenderRequest() {
|
||||
MockRenderRequest request = new MockRenderRequest();
|
||||
applyPortletRequestSecurity(request);
|
||||
return request;
|
||||
MockRenderRequest request = new MockRenderRequest();
|
||||
applyPortletRequestSecurity(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
public static MockRenderResponse createRenderResponse() {
|
||||
MockRenderResponse response = new MockRenderResponse();
|
||||
return response;
|
||||
MockRenderResponse response = new MockRenderResponse();
|
||||
return response;
|
||||
}
|
||||
|
||||
public static MockActionRequest createActionRequest() {
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
applyPortletRequestSecurity(request);
|
||||
return request;
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
applyPortletRequestSecurity(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
public static MockActionResponse createActionResponse() {
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
return response;
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
return response;
|
||||
}
|
||||
|
||||
public static PreAuthenticatedAuthenticationToken createToken(PortletRequest request) {
|
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(TESTUSER, TESTCRED);
|
||||
token.setDetails(new PortletAuthenticationDetails(request));
|
||||
return token;
|
||||
}
|
||||
public static PreAuthenticatedAuthenticationToken createToken(PortletRequest request) {
|
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(TESTUSER, TESTCRED);
|
||||
token.setDetails(new PortletAuthenticationDetails(request));
|
||||
return token;
|
||||
}
|
||||
|
||||
public static PreAuthenticatedAuthenticationToken createToken() {
|
||||
MockRenderRequest request = createRenderRequest();
|
||||
return createToken(request);
|
||||
}
|
||||
public static PreAuthenticatedAuthenticationToken createToken() {
|
||||
MockRenderRequest request = createRenderRequest();
|
||||
return createToken(request);
|
||||
}
|
||||
|
||||
public static PreAuthenticatedAuthenticationToken createAuthenticatedToken(UserDetails user) {
|
||||
PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(
|
||||
user, user.getPassword(), user.getAuthorities());
|
||||
result.setAuthenticated(true);
|
||||
return result;
|
||||
}
|
||||
public static PreAuthenticatedAuthenticationToken createAuthenticatedToken(UserDetails user) {
|
||||
PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(
|
||||
user, user.getPassword(), user.getAuthorities().toArray(new GrantedAuthority[0]));
|
||||
result.setAuthenticated(true);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static PreAuthenticatedAuthenticationToken createAuthenticatedToken() {
|
||||
return createAuthenticatedToken(createUser());
|
||||
}
|
||||
public static PreAuthenticatedAuthenticationToken createAuthenticatedToken() {
|
||||
return createAuthenticatedToken(createUser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+179
-179
@@ -50,236 +50,236 @@ import org.springframework.mock.web.portlet.MockRenderResponse;
|
||||
*/
|
||||
public class PortletProcessingInterceptorTests extends TestCase {
|
||||
|
||||
//~ Constructors ===================================================================================================
|
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
public PortletProcessingInterceptorTests() {
|
||||
super();
|
||||
}
|
||||
public PortletProcessingInterceptorTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public PortletProcessingInterceptorTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
public PortletProcessingInterceptorTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
public void testRequiresAuthenticationManager() throws Exception {
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
|
||||
try {
|
||||
interceptor.afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
public void testRequiresAuthenticationManager() throws Exception {
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
|
||||
public void testNormalRenderRequestProcessing() throws Exception {
|
||||
try {
|
||||
interceptor.afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
// Build mock request and response
|
||||
MockRenderRequest request = PortletTestUtils.createRenderRequest();
|
||||
MockRenderResponse response = PortletTestUtils.createRenderResponse();
|
||||
public void testNormalRenderRequestProcessing() throws Exception {
|
||||
|
||||
// Prepare interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
// Build mock request and response
|
||||
MockRenderRequest request = PortletTestUtils.createRenderRequest();
|
||||
MockRenderResponse response = PortletTestUtils.createRenderResponse();
|
||||
|
||||
// Execute preHandlerRender phase and verify results
|
||||
interceptor.preHandleRender(request, response, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
// Prepare interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
|
||||
// Execute postHandlerRender phase and verify nothing changed
|
||||
interceptor.postHandleRender(request, response, null, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
// Execute preHandlerRender phase and verify results
|
||||
interceptor.preHandleRender(request, response, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
// Execute afterRenderCompletion phase and verify nothing changed
|
||||
interceptor.afterRenderCompletion(request, response, null, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
// Execute postHandlerRender phase and verify nothing changed
|
||||
interceptor.postHandleRender(request, response, null, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
public void testNormalActionRequestProcessing() throws Exception {
|
||||
// Execute afterRenderCompletion phase and verify nothing changed
|
||||
interceptor.afterRenderCompletion(request, response, null, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
// Build mock request and response
|
||||
MockActionRequest request = PortletTestUtils.createActionRequest();
|
||||
MockActionResponse response = PortletTestUtils.createActionResponse();
|
||||
public void testNormalActionRequestProcessing() throws Exception {
|
||||
|
||||
// Prepare interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
// Build mock request and response
|
||||
MockActionRequest request = PortletTestUtils.createActionRequest();
|
||||
MockActionResponse response = PortletTestUtils.createActionResponse();
|
||||
|
||||
// Execute preHandlerAction phase and verify results
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
// Prepare interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
|
||||
// Execute afterActionCompletion phase and verify nothing changed
|
||||
interceptor.afterActionCompletion(request, response, null, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
// Execute preHandlerAction phase and verify results
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
public void testAuthenticationFailsWithNoCredentials()
|
||||
throws Exception {
|
||||
// Execute afterActionCompletion phase and verify nothing changed
|
||||
interceptor.afterActionCompletion(request, response, null, null);
|
||||
assertEquals(PortletTestUtils.createAuthenticatedToken(),
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
public void testAuthenticationFailsWithNoCredentials()
|
||||
throws Exception {
|
||||
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
|
||||
// Verify that authentication is empty
|
||||
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
|
||||
// Verify that proper exception was thrown
|
||||
assertTrue(request.getPortletSession().getAttribute(
|
||||
AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY,
|
||||
PortletSession.APPLICATION_SCOPE)
|
||||
instanceof BadCredentialsException);
|
||||
}
|
||||
// Verify that authentication is empty
|
||||
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
public void testExistingAuthenticationIsLeftAlone() throws Exception {
|
||||
// Verify that proper exception was thrown
|
||||
assertTrue(request.getPortletSession().getAttribute(
|
||||
AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY,
|
||||
PortletSession.APPLICATION_SCOPE)
|
||||
instanceof BadCredentialsException);
|
||||
}
|
||||
|
||||
// Build mock request and response
|
||||
MockActionRequest request = PortletTestUtils.createActionRequest();
|
||||
MockActionResponse response = PortletTestUtils.createActionResponse();
|
||||
public void testExistingAuthenticationIsLeftAlone() throws Exception {
|
||||
|
||||
// Prepare interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
// Build mock request and response
|
||||
MockActionRequest request = PortletTestUtils.createActionRequest();
|
||||
MockActionResponse response = PortletTestUtils.createActionResponse();
|
||||
|
||||
UsernamePasswordAuthenticationToken testingToken = new UsernamePasswordAuthenticationToken("dummy", "dummy");
|
||||
UsernamePasswordAuthenticationToken baselineToken = new UsernamePasswordAuthenticationToken("dummy", "dummy");
|
||||
SecurityContextHolder.getContext().setAuthentication(testingToken);
|
||||
// Prepare interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
|
||||
// Execute preHandlerAction phase and verify results
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
assertTrue(SecurityContextHolder.getContext().getAuthentication() == testingToken);
|
||||
assertEquals(baselineToken, SecurityContextHolder.getContext().getAuthentication());
|
||||
UsernamePasswordAuthenticationToken testingToken = new UsernamePasswordAuthenticationToken("dummy", "dummy");
|
||||
UsernamePasswordAuthenticationToken baselineToken = new UsernamePasswordAuthenticationToken("dummy", "dummy");
|
||||
SecurityContextHolder.getContext().setAuthentication(testingToken);
|
||||
|
||||
// Execute afterActionCompletion phase and verify nothing changed
|
||||
interceptor.afterActionCompletion(request, response, null, null);
|
||||
assertTrue(SecurityContextHolder.getContext().getAuthentication() == testingToken);
|
||||
assertEquals(baselineToken, SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
// Execute preHandlerAction phase and verify results
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
assertTrue(SecurityContextHolder.getContext().getAuthentication() == testingToken);
|
||||
assertEquals(baselineToken, SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
public void testUsernameFromRemoteUser() throws Exception {
|
||||
// Execute afterActionCompletion phase and verify nothing changed
|
||||
interceptor.afterActionCompletion(request, response, null, null);
|
||||
assertTrue(SecurityContextHolder.getContext().getAuthentication() == testingToken);
|
||||
assertEquals(baselineToken, SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
request.setRemoteUser(PortletTestUtils.TESTUSER);
|
||||
request.setAuthType(PortletRequest.FORM_AUTH);
|
||||
public void testUsernameFromRemoteUser() throws Exception {
|
||||
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
request.setRemoteUser(PortletTestUtils.TESTUSER);
|
||||
request.setAuthType(PortletRequest.FORM_AUTH);
|
||||
|
||||
// Verify username
|
||||
assertEquals(PortletTestUtils.TESTUSER,
|
||||
SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
}
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
|
||||
public void testUsernameFromPrincipal() throws Exception {
|
||||
// Verify username
|
||||
assertEquals(PortletTestUtils.TESTUSER,
|
||||
SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
}
|
||||
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
request.setUserPrincipal(new TestingAuthenticationToken(PortletTestUtils.TESTUSER, PortletTestUtils.TESTCRED));
|
||||
request.setAuthType(PortletRequest.FORM_AUTH);
|
||||
public void testUsernameFromPrincipal() throws Exception {
|
||||
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
request.setUserPrincipal(new TestingAuthenticationToken(PortletTestUtils.TESTUSER, PortletTestUtils.TESTCRED));
|
||||
request.setAuthType(PortletRequest.FORM_AUTH);
|
||||
|
||||
// Verify username
|
||||
assertEquals(PortletTestUtils.TESTUSER,
|
||||
SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
}
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
|
||||
public void testUsernameFromUserInfo() throws Exception {
|
||||
// Verify username
|
||||
assertEquals(PortletTestUtils.TESTUSER,
|
||||
SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
}
|
||||
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
HashMap userInfo = new HashMap();
|
||||
userInfo.put("user.name.given", "Test");
|
||||
userInfo.put("user.name.family", "User");
|
||||
userInfo.put("user.id", "mytestuser");
|
||||
request.setAttribute(PortletRequest.USER_INFO, userInfo);
|
||||
request.setAuthType(PortletRequest.FORM_AUTH);
|
||||
public void testUsernameFromUserInfo() throws Exception {
|
||||
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
ArrayList userNameAttributes = new ArrayList();
|
||||
userNameAttributes.add("user.name");
|
||||
userNameAttributes.add("user.id");
|
||||
interceptor.setUserNameAttributes(userNameAttributes);
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
// Build mock request and response
|
||||
MockActionRequest request = new MockActionRequest();
|
||||
MockActionResponse response = new MockActionResponse();
|
||||
HashMap userInfo = new HashMap();
|
||||
userInfo.put("user.name.given", "Test");
|
||||
userInfo.put("user.name.family", "User");
|
||||
userInfo.put("user.id", "mytestuser");
|
||||
request.setAttribute(PortletRequest.USER_INFO, userInfo);
|
||||
request.setAuthType(PortletRequest.FORM_AUTH);
|
||||
|
||||
// Verify username
|
||||
assertEquals("mytestuser", SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
}
|
||||
// Prepare and execute interceptor
|
||||
PortletProcessingInterceptor interceptor = new PortletProcessingInterceptor();
|
||||
interceptor.setAuthenticationManager(new MockPortletAuthenticationManager());
|
||||
ArrayList userNameAttributes = new ArrayList();
|
||||
userNameAttributes.add("user.name");
|
||||
userNameAttributes.add("user.id");
|
||||
interceptor.setUserNameAttributes(userNameAttributes);
|
||||
interceptor.afterPropertiesSet();
|
||||
interceptor.preHandleAction(request, response, null);
|
||||
|
||||
//~ Inner Classes ==================================================================================================
|
||||
// Verify username
|
||||
assertEquals("mytestuser", SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
}
|
||||
|
||||
private static class MockPortletAuthenticationManager implements AuthenticationManager {
|
||||
//~ Inner Classes ==================================================================================================
|
||||
|
||||
public Authentication authenticate(Authentication token) {
|
||||
private static class MockPortletAuthenticationManager implements AuthenticationManager {
|
||||
|
||||
// Make sure we got a valid token
|
||||
if (!(token instanceof PreAuthenticatedAuthenticationToken)) {
|
||||
TestCase.fail("Expected PreAuthenticatedAuthenticationToken object-- got: " + token);
|
||||
}
|
||||
public Authentication authenticate(Authentication token) {
|
||||
|
||||
// Make sure the token details are the PortletRequest
|
||||
// Make sure we got a valid token
|
||||
if (!(token instanceof PreAuthenticatedAuthenticationToken)) {
|
||||
TestCase.fail("Expected PreAuthenticatedAuthenticationToken object-- got: " + token);
|
||||
}
|
||||
|
||||
// Make sure the token details are the PortletRequest
|
||||
// if (!(token.getDetails() instanceof PortletRequest)) {
|
||||
// TestCase.fail("Expected Authentication.getDetails to be a PortletRequest object -- got: " + token.getDetails());
|
||||
// }
|
||||
|
||||
// Make sure it's got a principal
|
||||
if (token.getPrincipal() == null) {
|
||||
throw new BadCredentialsException("Mock authentication manager rejecting null principal");
|
||||
}
|
||||
// Make sure it's got a principal
|
||||
if (token.getPrincipal() == null) {
|
||||
throw new BadCredentialsException("Mock authentication manager rejecting null principal");
|
||||
}
|
||||
|
||||
// Make sure it's got credentials
|
||||
if (token.getCredentials() == null) {
|
||||
throw new BadCredentialsException("Mock authentication manager rejecting null credentials");
|
||||
}
|
||||
// Make sure it's got credentials
|
||||
if (token.getCredentials() == null) {
|
||||
throw new BadCredentialsException("Mock authentication manager rejecting null credentials");
|
||||
}
|
||||
|
||||
// create resulting Authentication object
|
||||
User user = new User(token.getName(), token.getCredentials().toString(), true, true, true, true,
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl(PortletTestUtils.TESTROLE1), new GrantedAuthorityImpl(PortletTestUtils.TESTROLE2)});
|
||||
PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(
|
||||
user, user.getPassword(), user.getAuthorities());
|
||||
result.setAuthenticated(true);
|
||||
return result;
|
||||
}
|
||||
// create resulting Authentication object
|
||||
User user = new User(token.getName(), token.getCredentials().toString(), true, true, true, true,
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl(PortletTestUtils.TESTROLE1), new GrantedAuthorityImpl(PortletTestUtils.TESTROLE2)});
|
||||
PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(
|
||||
user, user.getPassword(), user.getAuthorities().toArray(new GrantedAuthority[0]));
|
||||
result.setAuthenticated(true);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user