本文整理汇总了Java中org.springframework.mock.web.MockHttpServletRequest.setSession方法的典型用法代码示例。如果您正苦于以下问题:Java MockHttpServletRequest.setSession方法的具体用法?Java MockHttpServletRequest.setSession怎么用?Java MockHttpServletRequest.setSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.mock.web.MockHttpServletRequest
的用法示例。
在下文中一共展示了MockHttpServletRequest.setSession方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: verifyOKWithState
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyOKWithState() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + '?' + OAuthConstants.CODE + '=' + SERVICE_TICKET + '&' + OAuthConstants.STATE + '='
+ STATE, map.get("callbackUrl"));
}
示例2: testAddEventWithWebAuthenticationDetails
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testAddEventWithWebAuthenticationDetails() {
HttpSession session = new MockHttpSession(null, "test-session-id");
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(session);
request.setRemoteAddr("1.2.3.4");
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
Map<String, Object> data = new HashMap<>();
data.put("test-key", details);
AuditEvent event = new AuditEvent("test-user", "test-type", data);
customAuditEventRepository.add(event);
List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
assertThat(persistentAuditEvents).hasSize(1);
PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
示例3: verifyStartAuthentication
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyStartAuthentication() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, MY_THEME);
mockRequest.setParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, MY_LOCALE);
mockRequest.setParameter(CasProtocolConstants.PARAMETER_METHOD, MY_METHOD);
final MockHttpSession mockSession = new MockHttpSession();
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
mockRequestContext.getFlowScope().put(CasProtocolConstants.PARAMETER_SERVICE,
org.jasig.cas.services.TestUtils.getService(MY_SERVICE));
final FacebookClient facebookClient = new FacebookClient(MY_KEY, MY_SECRET);
final TwitterClient twitterClient = new TwitterClient(MY_KEY, MY_SECRET);
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient, twitterClient);
final ClientAction action = new ClientAction();
action.setCentralAuthenticationService(mock(CentralAuthenticationService.class));
action.setClients(clients);
final Event event = action.execute(mockRequestContext);
assertEquals("error", event.getId());
assertEquals(MY_THEME, mockSession.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_LOCALE, mockSession.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_METHOD, mockSession.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
final Map<String, String> urls = (Map<String, String>) flowScope.get(ClientAction.PAC4J_URLS);
assertTrue((urls.get("Facebook"))
.startsWith("https://www.facebook.com/v2.2/dialog/oauth?client_id=my_key&redirect_uri=http%3A%2F%2Fcasserver%2Flogin%3F"
+ Clients.DEFAULT_CLIENT_NAME_PARAMETER + "%3DFacebookClient&state="));
assertEquals(MY_LOGIN_URL + '?' + Clients.DEFAULT_CLIENT_NAME_PARAMETER
+ "=TwitterClient&needs_client_redirection=true", urls.get("Twitter"));
}
示例4: verifyOK
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyOK() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + '?' + OAuthConstants.CODE + '=' + SERVICE_TICKET, map.get("callbackUrl"));
}
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:20,代码来源:OAuth20CallbackAuthorizeControllerTests.java
示例5: verifyOK
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyOK() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
oauth20WrapperController.afterPropertiesSet();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET, map.get("callbackUrl"));
}
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:21,代码来源:OAuth20CallbackAuthorizeControllerTests.java
示例6: testOKWithState
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testOKWithState() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
oauth20WrapperController.afterPropertiesSet();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET + "&" + OAuthConstants.STATE + "="
+ STATE, map.get("callbackUrl"));
}
示例7: verifyStartAuthentication
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyStartAuthentication() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(ClientAction.THEME, MY_THEME);
mockRequest.setParameter(ClientAction.LOCALE, MY_LOCALE);
mockRequest.setParameter(ClientAction.METHOD, MY_METHOD);
final MockHttpSession mockSession = new MockHttpSession();
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
mockRequestContext.getFlowScope().put(ClientAction.SERVICE, new SimpleWebApplicationServiceImpl(MY_SERVICE));
final FacebookClient facebookClient = new FacebookClient(MY_KEY, MY_SECRET);
final TwitterClient twitterClient = new TwitterClient(MY_KEY, MY_SECRET);
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient, twitterClient);
final ClientAction action = new ClientAction(mock(CentralAuthenticationService.class), clients);
final Event event = action.execute(mockRequestContext);
assertEquals("error", event.getId());
assertEquals(MY_THEME, mockSession.getAttribute(ClientAction.THEME));
assertEquals(MY_LOCALE, mockSession.getAttribute(ClientAction.LOCALE));
assertEquals(MY_METHOD, mockSession.getAttribute(ClientAction.METHOD));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
assertTrue(((String) flowScope.get("FacebookClientUrl"))
.startsWith("https://www.facebook.com/v2.2/dialog/oauth?client_id=my_key&redirect_uri=http%3A%2F%2Fcasserver%2Flogin%3F"
+ Clients.DEFAULT_CLIENT_NAME_PARAMETER + "%3DFacebookClient&state="));
assertEquals(MY_LOGIN_URL + "?" + Clients.DEFAULT_CLIENT_NAME_PARAMETER
+ "=TwitterClient&needs_client_redirection=true", flowScope.get("TwitterClientUrl"));
}
示例8: verifyFinishAuthentication
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyFinishAuthentication() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(Clients.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
final MockHttpSession mockSession = new MockHttpSession();
mockSession.setAttribute(ClientAction.THEME, MY_THEME);
mockSession.setAttribute(ClientAction.LOCALE, MY_LOCALE);
mockSession.setAttribute(ClientAction.METHOD, MY_METHOD);
final Service service = new SimpleWebApplicationServiceImpl(MY_SERVICE);
mockSession.setAttribute(ClientAction.SERVICE, service);
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
final FacebookClient facebookClient = new MockFacebookClient();
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);
final TicketGrantingTicket tgt = new TicketGrantingTicketImpl(TGT_ID, mock(Authentication.class), mock(ExpirationPolicy.class));
final CentralAuthenticationService casImpl = mock(CentralAuthenticationService.class);
when(casImpl.createTicketGrantingTicket(any(Credential.class))).thenReturn(tgt);
final ClientAction action = new ClientAction(casImpl, clients);
final Event event = action.execute(mockRequestContext);
assertEquals("success", event.getId());
assertEquals(MY_THEME, mockRequest.getAttribute(ClientAction.THEME));
assertEquals(MY_LOCALE, mockRequest.getAttribute(ClientAction.LOCALE));
assertEquals(MY_METHOD, mockRequest.getAttribute(ClientAction.METHOD));
assertEquals(MY_SERVICE, mockRequest.getAttribute(ClientAction.SERVICE));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
final MutableAttributeMap requestScope = mockRequestContext.getRequestScope();
assertEquals(service, flowScope.get(ClientAction.SERVICE));
assertEquals(TGT_ID, flowScope.get(TGT_NAME));
assertEquals(TGT_ID, requestScope.get(TGT_NAME));
}
示例9: verifyStartAuthentication
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyStartAuthentication() throws Exception {
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, MY_THEME);
mockRequest.setParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, MY_LOCALE);
mockRequest.setParameter(CasProtocolConstants.PARAMETER_METHOD, MY_METHOD);
final MockHttpSession mockSession = new MockHttpSession();
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
when(servletExternalContext.getNativeResponse()).thenReturn(mockResponse);
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
mockRequestContext.getFlowScope().put(CasProtocolConstants.PARAMETER_SERVICE,
RegisteredServiceTestUtils.getService(MY_SERVICE));
final FacebookClient facebookClient = new FacebookClient(MY_KEY, MY_SECRET);
final TwitterClient twitterClient = new TwitterClient("3nJPbVTVRZWAyUgoUKQ8UA", "h6LZyZJmcW46Vu8R47MYfeXTSYGI30EqnWaSwVhFkbA");
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient, twitterClient);
final DelegatedClientAuthenticationAction action = new DelegatedClientAuthenticationAction(clients,
null, mock(CentralAuthenticationService.class),
"theme", "locale", false);
final Event event = action.execute(mockRequestContext);
assertEquals("error", event.getId());
assertEquals(MY_THEME, mockSession.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_LOCALE, mockSession.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_METHOD, mockSession.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
final Set<DelegatedClientAuthenticationAction.ProviderLoginPageConfiguration> urls =
(Set<DelegatedClientAuthenticationAction.ProviderLoginPageConfiguration>) flowScope.get(DelegatedClientAuthenticationAction.PAC4J_URLS);
assertFalse(urls.isEmpty());
assertSame(2, urls.size());
}
示例10: verifyFinishAuthentication
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyFinishAuthentication() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(Clients.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
final MockHttpSession mockSession = new MockHttpSession();
mockSession.setAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, MY_THEME);
mockSession.setAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, MY_LOCALE);
mockSession.setAttribute(CasProtocolConstants.PARAMETER_METHOD, MY_METHOD);
final Service service = org.jasig.cas.authentication.TestUtils.getService(MY_SERVICE);
mockSession.setAttribute(CasProtocolConstants.PARAMETER_SERVICE, service);
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
final FacebookClient facebookClient = new MockFacebookClient();
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);
final TicketGrantingTicket tgt = new TicketGrantingTicketImpl(TGT_ID, mock(Authentication.class), mock(ExpirationPolicy.class));
final CentralAuthenticationService casImpl = mock(CentralAuthenticationService.class);
when(casImpl.createTicketGrantingTicket(any(AuthenticationContext.class))).thenReturn(tgt);
final ClientAction action = new ClientAction();
final AuthenticationManager authNManager = mock(AuthenticationManager.class);
when(authNManager.authenticate(any(AuthenticationTransaction.class))).thenReturn(TestUtils.getAuthentication());
action.getAuthenticationSystemSupport().getAuthenticationTransactionManager()
.setAuthenticationManager(authNManager);
action.setCentralAuthenticationService(casImpl);
action.setClients(clients);
final Event event = action.execute(mockRequestContext);
assertEquals("success", event.getId());
assertEquals(MY_THEME, mockRequest.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_LOCALE, mockRequest.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_METHOD, mockRequest.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
assertEquals(MY_SERVICE, mockRequest.getAttribute(CasProtocolConstants.PARAMETER_SERVICE));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
final MutableAttributeMap requestScope = mockRequestContext.getRequestScope();
assertEquals(service, flowScope.get(CasProtocolConstants.PARAMETER_SERVICE));
assertEquals(TGT_ID, flowScope.get(TGT_NAME));
assertEquals(TGT_ID, requestScope.get(TGT_NAME));
}
示例11: verifyFinishAuthentication
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyFinishAuthentication() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(Clients.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
final MockHttpSession mockSession = new MockHttpSession();
mockSession.setAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, MY_THEME);
mockSession.setAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, MY_LOCALE);
mockSession.setAttribute(CasProtocolConstants.PARAMETER_METHOD, MY_METHOD);
final Service service = CoreAuthenticationTestUtils.getService(MY_SERVICE);
mockSession.setAttribute(CasProtocolConstants.PARAMETER_SERVICE, service);
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
when(servletExternalContext.getNativeResponse()).thenReturn(new MockHttpServletResponse());
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
final FacebookClient facebookClient = new FacebookClient() {
@Override
protected OAuth20Credentials retrieveCredentials(final WebContext context) throws HttpAction {
return new OAuth20Credentials("fakeVerifier", FacebookClient.class.getSimpleName());
}
};
facebookClient.setName(FacebookClient.class.getSimpleName());
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);
final TicketGrantingTicket tgt = new TicketGrantingTicketImpl(TGT_ID, mock(Authentication.class), mock(ExpirationPolicy.class));
final CentralAuthenticationService casImpl = mock(CentralAuthenticationService.class);
when(casImpl.createTicketGrantingTicket(any())).thenReturn(tgt);
final AuthenticationTransactionManager transManager = mock(AuthenticationTransactionManager.class);
final AuthenticationManager authNManager = mock(AuthenticationManager.class);
when(authNManager.authenticate(any(AuthenticationTransaction.class))).thenReturn(CoreAuthenticationTestUtils.getAuthentication());
when(transManager.getAuthenticationManager()).thenReturn(authNManager);
when(transManager.handle(any(AuthenticationTransaction.class), any(AuthenticationResultBuilder.class))).thenReturn(transManager);
final AuthenticationSystemSupport support = mock(AuthenticationSystemSupport.class);
when(support.getAuthenticationTransactionManager()).thenReturn(transManager);
final DelegatedClientAuthenticationAction action = new DelegatedClientAuthenticationAction(clients, support, casImpl,
"theme", "locale", false);
final Event event = action.execute(mockRequestContext);
assertEquals("success", event.getId());
assertEquals(MY_THEME, mockRequest.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_LOCALE, mockRequest.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(MY_METHOD, mockRequest.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
assertEquals(MY_SERVICE, mockRequest.getAttribute(CasProtocolConstants.PARAMETER_SERVICE));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
final MutableAttributeMap requestScope = mockRequestContext.getRequestScope();
assertEquals(service, flowScope.get(CasProtocolConstants.PARAMETER_SERVICE));
assertEquals(TGT_ID, flowScope.get(TGT_NAME));
assertEquals(TGT_ID, requestScope.get(TGT_NAME));
}
示例12: verifyCodeRedirectToClientWithState
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyCodeRedirectToClientWithState() throws Exception {
clearAllServices();
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuth20Constants.AUTHORIZE_URL);
mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
mockRequest.setParameter(OAuth20Constants.RESPONSE_TYPE, OAuth20ResponseTypes.CODE.name().toLowerCase());
mockRequest.setServerName(CAS_SERVER);
mockRequest.setServerPort(CAS_PORT);
mockRequest.setScheme(CAS_SCHEME);
mockRequest.setParameter(OAuth20Constants.STATE, STATE);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, SERVICE_NAME);
service.setBypassApprovalPrompt(true);
this.servicesManager.save(service);
final CasProfile profile = new CasProfile();
profile.setId(ID);
final Map<String, Object> attributes = new HashMap<>();
attributes.put(FIRST_NAME_ATTRIBUTE, FIRST_NAME);
attributes.put(LAST_NAME_ATTRIBUTE, LAST_NAME);
profile.addAttributes(attributes);
final MockHttpSession session = new MockHttpSession();
mockRequest.setSession(session);
session.putValue(Pac4jConstants.USER_PROFILES, profile);
final ModelAndView modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
final View view = modelAndView.getView();
assertTrue(view instanceof RedirectView);
final RedirectView redirectView = (RedirectView) view;
final String redirectUrl = redirectView.getUrl();
assertTrue(redirectUrl.startsWith(REDIRECT_URI + "?code=OC-"));
final String code = StringUtils.substringBefore(StringUtils.substringAfter(redirectUrl, "?code="), "&state=");
final OAuthCode oAuthCode = (OAuthCode) this.ticketRegistry.getTicket(code);
assertNotNull(oAuthCode);
final Principal principal = oAuthCode.getAuthentication().getPrincipal();
assertEquals(ID, principal.getId());
final Map<String, Object> principalAttributes = principal.getAttributes();
assertEquals(attributes.size(), principalAttributes.size());
assertEquals(FIRST_NAME, principalAttributes.get(FIRST_NAME_ATTRIBUTE));
}
示例13: verifyCodeRedirectToClientApproved
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyCodeRedirectToClientApproved() throws Exception {
clearAllServices();
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuth20Constants.AUTHORIZE_URL);
mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
mockRequest.setParameter(OAuth20Constants.RESPONSE_TYPE, OAuth20ResponseTypes.CODE.name().toLowerCase());
mockRequest.setServerName(CAS_SERVER);
mockRequest.setServerPort(CAS_PORT);
mockRequest.setScheme(CAS_SCHEME);
mockRequest.setParameter(OAuth20Constants.BYPASS_APPROVAL_PROMPT, "true");
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, SERVICE_NAME);
service.setBypassApprovalPrompt(false);
this.servicesManager.save(service);
final CasProfile profile = new CasProfile();
profile.setId(ID);
final Map<String, Object> attributes = new HashMap<>();
attributes.put(FIRST_NAME_ATTRIBUTE, FIRST_NAME);
attributes.put(LAST_NAME_ATTRIBUTE, LAST_NAME);
profile.addAttributes(attributes);
final MockHttpSession session = new MockHttpSession();
mockRequest.setSession(session);
session.putValue(Pac4jConstants.USER_PROFILES, profile);
final ModelAndView modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
final View view = modelAndView.getView();
assertTrue(view instanceof RedirectView);
final RedirectView redirectView = (RedirectView) view;
final String redirectUrl = redirectView.getUrl();
assertTrue(redirectUrl.startsWith(REDIRECT_URI + "?code=OC-"));
final String code = StringUtils.substringAfter(redirectUrl, "?code=");
final OAuthCode oAuthCode = (OAuthCode) this.ticketRegistry.getTicket(code);
assertNotNull(oAuthCode);
final Principal principal = oAuthCode.getAuthentication().getPrincipal();
assertEquals(ID, principal.getId());
final Map<String, Object> principalAttributes = principal.getAttributes();
assertEquals(attributes.size(), principalAttributes.size());
assertEquals(FIRST_NAME, principalAttributes.get(FIRST_NAME_ATTRIBUTE));
}
示例14: verifyTokenRedirectToClientApproved
import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyTokenRedirectToClientApproved() throws Exception {
clearAllServices();
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuth20Constants.AUTHORIZE_URL);
mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
mockRequest.setParameter(OAuth20Constants.RESPONSE_TYPE, OAuth20ResponseTypes.TOKEN.name().toLowerCase());
mockRequest.setServerName(CAS_SERVER);
mockRequest.setServerPort(CAS_PORT);
mockRequest.setScheme(CAS_SCHEME);
mockRequest.setParameter(OAuth20Constants.BYPASS_APPROVAL_PROMPT, "true");
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, SERVICE_NAME);
service.setBypassApprovalPrompt(false);
this.servicesManager.save(service);
final CasProfile profile = new CasProfile();
profile.setId(ID);
final Map<String, Object> attributes = new HashMap<>();
attributes.put(FIRST_NAME_ATTRIBUTE, FIRST_NAME);
attributes.put(LAST_NAME_ATTRIBUTE, LAST_NAME);
profile.addAttributes(attributes);
final MockHttpSession session = new MockHttpSession();
mockRequest.setSession(session);
session.putValue(Pac4jConstants.USER_PROFILES, profile);
final ModelAndView modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
final View view = modelAndView.getView();
assertTrue(view instanceof RedirectView);
final RedirectView redirectView = (RedirectView) view;
final String redirectUrl = redirectView.getUrl();
assertTrue(redirectUrl.startsWith(REDIRECT_URI + "#access_token="));
final String code = StringUtils.substringBetween(redirectUrl, "#access_token=", "&token_type=bearer");
final AccessToken accessToken = (AccessToken) this.ticketRegistry.getTicket(code);
assertNotNull(accessToken);
final Principal principal = accessToken.getAuthentication().getPrincipal();
assertEquals(ID, principal.getId());
final Map<String, Object> principalAttributes = principal.getAttributes();
assertEquals(attributes.size(), principalAttributes.size());
assertEquals(FIRST_NAME, principalAttributes.get(FIRST_NAME_ATTRIBUTE));
}