本文整理匯總了Java中org.springframework.security.authentication.AbstractAuthenticationToken類的典型用法代碼示例。如果您正苦於以下問題:Java AbstractAuthenticationToken類的具體用法?Java AbstractAuthenticationToken怎麽用?Java AbstractAuthenticationToken使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AbstractAuthenticationToken類屬於org.springframework.security.authentication包,在下文中一共展示了AbstractAuthenticationToken類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setUp
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
/**
* JAVADOC Method Level Comments
*
* @throws Exception JAVADOC.
*/
@Before
public void setUp()
throws Exception {
MockitoAnnotations.initMocks(this);
interceptor = new CurrentUserChannelInterceptor(systemUserService, userAccessor);
if (null == SecurityContextHolder.getContext()) {
SecurityContextHolder.setContext(new SecurityContextImpl());
}
SecurityContext context = SecurityContextHolder.getContext();
user = new User();
user.setName("user");
AbstractAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(user, null);
authToken.setDetails("pipipi");
context.setAuthentication(authToken);
}
示例2: authenticate
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// get username and password
String username = (authentication.getPrincipal() == null) ? "" : authentication.getName();
String password = (authentication.getCredentials() == null) ? "" : authentication.getCredentials().toString();
// check credentials
if (userService.checkCredentials(username, password)) {
// init return value
AbstractAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, null, new ArrayList<>());
// set user object
authenticationToken.setDetails(userService.getUserByUsername(username));
// return user details
return authenticationToken;
}
// indicate invalid credentials
throw new InternalAuthenticationServiceException("Unable to authenticate");
}
示例3: attemptAuthentication
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException,
IOException, ServletException {
String apiKeyValue = decodeParameterValue(request, API_KEY_PARAMETER_NAME);
logger.debug("attemptAuthentication " + apiKeyValue);
AbstractAuthenticationToken authRequest = createAuthenticationToken(
apiKeyValue, new RestCredentials());
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
示例4: getOAuth2Authentication
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@Override
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
try {
Authentication userAuth = null;
User user = extensionGrantProvider.grant(convert(tokenRequest));
if (user != null) {
userAuth = new UsernamePasswordAuthenticationToken(user, "", AuthorityUtils.NO_AUTHORITIES);
if (extensionGrant.isCreateUser()) {
Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
parameters.put(RepositoryProviderUtils.SOURCE, extensionGrant.getIdentityProvider());
((AbstractAuthenticationToken) userAuth).setDetails(parameters);
eventPublisher.publishAuthenticationSuccess(userAuth);
}
}
OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, userAuth);
} catch (InvalidGrantException e) {
throw new org.springframework.security.oauth2.common.exceptions.InvalidGrantException(e.getMessage(), e);
}
}
示例5: createSuccessAuthentication
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
protected Authentication createSuccessAuthentication(UserDetails details,
Authentication authentication) {
if (details == null || authentication == null) {
return null;
}
AbstractAuthenticationToken auth = null;
if (authentication instanceof UsernamePasswordAuthenticationToken) {
auth = new UsernamePasswordAuthenticationToken(details,
authentication.getCredentials(), details.getAuthorities());
} else if (authentication instanceof ConfluenceAuthenticationToken) {
auth = new ConfluenceAuthenticationToken(details,
(String) authentication.getCredentials(), details.getAuthorities());
}
if (auth != null) {
auth.setDetails(authentication.getDetails());
}
return auth;
}
示例6: refreshUserContextIfActive
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@Override
@Transactional
public void refreshUserContextIfActive(String userName) {
LOGGER.info("Refreshing context for user: {}", userName);
MotechUser user = motechUsersDao.findByUserName(userName);
Collection<HttpSession> sessions = sessionHandler.getAllSessions();
for (HttpSession session : sessions) {
SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
if (context != null) {
Authentication authentication = context.getAuthentication();
AbstractAuthenticationToken token;
User userInSession = (User) authentication.getPrincipal();
if (userInSession.getUsername().equals(userName)) {
token = getToken(authentication, user);
context.setAuthentication(token);
}
}
}
LOGGER.info("Refreshed context for user: {}", userName);
}
示例7: attemptAuthentication
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
/**
* Attempt to authenticate request - basically just pass over to another method to authenticate request headers
*/
@Override
public Authentication attemptAuthentication(final HttpServletRequest request, final HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
String token = null;
if (null != request.getCookies()) {
for (final Cookie cookie : request.getCookies()) {
if (COOKIE_SECURITY_TOKEN.equals(cookie.getName())) {
token = cookie.getValue();
}
}
}
if (token == null) {
logger.info("No token found request:" + request.getRequestURI());
throw new AuthenticationServiceException(MessageFormat.format("Error | {0}", "No Token"));
}
logger.info("token found:" + token + " request:" + request.getRequestURI());
final AbstractAuthenticationToken userAuthenticationToken = authUserByToken(token);
if (userAuthenticationToken == null) {
throw new AuthenticationServiceException(MessageFormat.format("Error | {0}", "Bad Token"));
}
return userAuthenticationToken;
}
示例8: createContext
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
private SecurityContext createContext(final User user) {
SecurityContext securityContext = new SecurityContextImpl();
securityContext.setAuthentication(new AbstractAuthenticationToken(user.getAuthorities()) {
private static final long serialVersionUID = 1L;
@Override
public Object getCredentials() {
return "N/A";
}
@Override
public Object getPrincipal() {
return user;
}
@Override
public boolean isAuthenticated() {
return true;
}
});
return securityContext;
}
示例9: getAuthenticatedUser_validUser
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@Test
public void getAuthenticatedUser_validUser() {
final User authUser = new UserImpl(USER_ID);
AbstractAuthenticationToken auth = createNiceMock(AbstractAuthenticationToken.class);
expect(auth.getPrincipal()).andReturn(authUser).anyTimes();
replay(auth);
SecurityContext context = new SecurityContextImpl();
context.setAuthentication(auth);
SecurityContextHolder.setContext(context);
User result = service.getAuthenticatedUser();
assertThat(result, is(sameInstance(authUser)));
verify(auth);
}
示例10: setup
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Before
public void setup() throws SQLException {
restOperations = EasyMock.createNiceMock(RestOperations.class);
EasyMock.expect(restOperations.postForObject(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), EasyMock.anyObject(Class.class)))
.andReturn(VALID_METADATA);
EasyMock.replay(restOperations);
//Replace the real restOperations instance with a mock -- otherwise the call for gadget metadata would fail since
//we don't have a shindig server available to hit.
ReflectionTestUtils.setField(metadataRepository, "restOperations", restOperations);
//Setup a mock authenticated user
final User authUser = new UserImpl(VALID_USER_ID, VALID_USER_NAME);
AbstractAuthenticationToken auth = EasyMock.createNiceMock(AbstractAuthenticationToken.class);
EasyMock.expect(auth.getPrincipal()).andReturn(authUser).anyTimes();
EasyMock.replay(auth);
SecurityContext context = new SecurityContextImpl();
context.setAuthentication(auth);
SecurityContextHolder.setContext(context);
}
示例11: postUser
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@RequestMapping(value = "/rest/auth", method = RequestMethod.POST, produces = {"application/json"})
@ResponseBody
public AuthenticationResultDto postUser(@RequestParam("user") String user, HttpServletRequest request) {
AuthenticationResultDto dto = new AuthenticationResultDto();
dto.setSessionId(request.getSession().getId());
try {
// Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated
AbstractAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "");
token.setDetails(new WebAuthenticationDetails(request));
Authentication authentication = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
dto.setSuccess(Boolean.TRUE);
request.getSession().setAttribute("authenticated", Boolean.TRUE);
} catch (Exception e) {
SecurityContextHolder.getContext().setAuthentication(null);
dto.setSuccess(Boolean.FALSE);
request.getSession().setAttribute("authenticated", Boolean.FALSE);
}
return dto;
}
示例12: convertToAuthentication
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
protected Authentication convertToAuthentication(Subject subject) {
AbstractAuthenticationToken authToken = null;
Set<UsernamePasswordPrincipal> principalSet = subject.getPrincipals(UsernamePasswordPrincipal.class);
if (principalSet.size() > 0) {
UsernamePasswordPrincipal upp = principalSet.iterator().next();
authToken = new UsernamePasswordAuthenticationToken(upp.getName(), upp.getPassword());
}
if (authToken != null) {
Set<DomainPrincipal> auxset = subject.getPrincipals(DomainPrincipal.class);
if (auxset.size() > 0) {
String domain = auxset.iterator().next().getName();
authToken.setDetails(domain);
}
}
return authToken;
}
示例13: onApplicationEvent
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof AbstractAuthenticationFailureEvent) {
if (event.getSource() instanceof AbstractAuthenticationToken) {
AbstractAuthenticationToken token = (AbstractAuthenticationToken) event.getSource();
Object details = token.getDetails();
if (details instanceof WebAuthenticationDetails) {
LOG.info("Login failed from [" + ((WebAuthenticationDetails) details).getRemoteAddress() + "]");
}
}
}
}
示例14: setDetailsIfPossible
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
/**
* Set auth details if it possible
* @param authentication
* @param details
* @return true if update details is success
*/
public static boolean setDetailsIfPossible(Authentication authentication, Object details) {
if(authentication instanceof AbstractAuthenticationToken) {
((AbstractAuthenticationToken)authentication).setDetails(details);
return true;
}
return false;
}
示例15: test
import org.springframework.security.authentication.AbstractAuthenticationToken; //導入依賴的package包/類
/**
* JAVADOC Method Level Comments
*
* @throws Throwable JAVADOC.
*/
@Test
public void test()
throws Throwable {
//create authentication
User user = new User();
user.setUsername("loggedin");
//set security
AbstractAuthenticationToken authToken = setSecurity(user, true);
//mock systemUserService returns username
String systemUsername = "ADMIN";
when(systemUserService.getUsername()).thenReturn(systemUsername);
SystemUserMethodInterceptor interceptor = new SystemUserMethodInterceptor(userAccessor,
systemUserService);
interceptor.invoke(methodInvocation);
//mock authenticatioNService call
verify(userAccessor).forceUserToContext(systemUsername);
verify(methodInvocation).proceed();
//test it switches back
assertEquals(CurrentUserAccessor.currentAuthentication(), authToken);
}