本文整理汇总了Java中org.springframework.security.web.authentication.WebAuthenticationDetails类的典型用法代码示例。如果您正苦于以下问题:Java WebAuthenticationDetails类的具体用法?Java WebAuthenticationDetails怎么用?Java WebAuthenticationDetails使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebAuthenticationDetails类属于org.springframework.security.web.authentication包,在下文中一共展示了WebAuthenticationDetails类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAddEventWithWebAuthenticationDetails
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的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");
}
示例2: handle
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
@Override
public void handle(Object event) {
AuthenticationFailureBadCredentialsEvent loginFailureEvent = (AuthenticationFailureBadCredentialsEvent) event;
Object name = loginFailureEvent.getAuthentication().getPrincipal();
Users user = usersRepository.loadUserByUsername((String) name);
eventService.raiseSecurityEvent(new AuthenticationFailedEvent(
((WebAuthenticationDetails) loginFailureEvent
.getAuthentication().getDetails()).getRemoteAddress(),
(String) name));
if (user != null) {
// update the failed login count
user.increaseFailedLoginAttempts();
if (user.getFailedLoginAttempts() >= max_failed_attempts) {
Calendar cal = Calendar.getInstance();
user.setLockoutTime(cal);
}
// update user
usersRepository.updateUser(user);
}
}
示例3: convertDataToStrings
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
/**
* Internal conversion. This method will allow to save additional data.
* By default, it will save the object as string
*
* @param data the data to convert
* @return a map of String, String
*/
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
Map<String, String> results = new HashMap<>();
if (data != null) {
for (Map.Entry<String, Object> entry : data.entrySet()) {
Object object = entry.getValue();
// Extract the data that will be saved.
if (object instanceof WebAuthenticationDetails) {
WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
results.put("remoteAddress", authenticationDetails.getRemoteAddress());
results.put("sessionId", authenticationDetails.getSessionId());
} else if (object != null) {
results.put(entry.getKey(), object.toString());
} else {
results.put(entry.getKey(), "null");
}
}
}
return results;
}
示例4: testHandle_when_account_is_not_locked
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
@Test
public void testHandle_when_account_is_not_locked() {
AuthenticationFailureBadCredentialsEvent loginFailureEvent=mock(AuthenticationFailureBadCredentialsEvent.class);
Authentication authentication=mock(Authentication.class);
WebAuthenticationDetails webAuthenticationDetails=mock(WebAuthenticationDetails.class);
Object name=new String(username);
when(loginFailureEvent.getAuthentication()).thenReturn(authentication);
when(authentication.getDetails()).thenReturn(webAuthenticationDetails);
when(webAuthenticationDetails.getRemoteAddress()).thenReturn("127.0.0.1");
when(authentication.getPrincipal()).thenReturn(name);
Users user=mock(Users.class);
when(user.getFailedLoginAttempts()).thenReturn(0);
when(usersRepository.loadUserByUsername(username)).thenReturn(user);
loginFailureEventListener.handle(loginFailureEvent);
verify(usersRepository).updateUser(user);
}
示例5: testHandle_when_account_is_locked
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
@Test
public void testHandle_when_account_is_locked() {
AuthenticationFailureBadCredentialsEvent loginFailureEvent=mock(AuthenticationFailureBadCredentialsEvent.class);
Authentication authentication=mock(Authentication.class);
WebAuthenticationDetails webAuthenticationDetails=mock(WebAuthenticationDetails.class);
Object name=new String(username);
when(loginFailureEvent.getAuthentication()).thenReturn(authentication);
when(authentication.getPrincipal()).thenReturn(name);
when(authentication.getDetails()).thenReturn(webAuthenticationDetails);
when(webAuthenticationDetails.getRemoteAddress()).thenReturn("127.0.0.1");
Users user=mock(Users.class);
when(user.getFailedLoginAttempts()).thenReturn(3);
when(usersRepository.loadUserByUsername(username)).thenReturn(user);
loginFailureEventListener.handle(loginFailureEvent);
verify(user).setLockoutTime(any(Calendar.class));
verify(usersRepository).updateUser(user);
}
示例6: onApplicationEvent
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
@Override
public void onApplicationEvent(AbstractAuthenticationEvent event) {
if (event instanceof AuthenticationSuccessEvent) {
log.debug("Authentication OK: {}", event.getAuthentication().getName());
// Activity log
Object details = event.getAuthentication().getDetails();
String params = null;
if (details instanceof WebAuthenticationDetails) {
WebAuthenticationDetails wad = (WebAuthenticationDetails) details;
params = wad.getRemoteAddress();
} else if (GenericHolder.get() != null) {
params = (String) GenericHolder.get();
}
UserActivity.log(event.getAuthentication().getName(), "LOGIN", null, null, params);
} else if (event instanceof AuthenticationFailureBadCredentialsEvent) {
log.info("Authentication ERROR: {}", event.getAuthentication().getName());
}
}
示例7: onApplicationEvent
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
@Override
public void onApplicationEvent(AbstractSubProtocolEvent ev) {
if(ev instanceof SessionSubscribeEvent) {
sendHistoryToNewSubscriber(ev);
} else if(ev instanceof SessionConnectEvent || ev instanceof SessionDisconnectEvent) {
Authentication user = (Authentication)ev.getUser();
Object details = user.getDetails();
String sessionId = null;
String address = null;
if(details instanceof WebAuthenticationDetails) {
WebAuthenticationDetails wad = (WebAuthenticationDetails) details;
address = wad.getRemoteAddress();
sessionId = wad.getSessionId();
}
if(ev instanceof SessionDisconnectEvent) {
log.info("WebSocket user \"{}\" was disconnected from {} with HTTP session: {}", user.getName(), address, sessionId);
} else {
log.info("WebSocket user \"{}\" was connected from {} with HTTP session: {}", user.getName(), address, sessionId);
}
}
}
示例8: convertDataToStrings
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
/**
* Internal conversion. This method will allow to save additional data.
* By default, it will save the object as string
*
* @param data the data to convert
* @return a map of String, String
*/
public Map<String, String> convertDataToStrings(Map<String, Object> data) {
Map<String, String> results = new HashMap<>();
if (data != null) {
for (String key : data.keySet()) {
Object object = data.get(key);
// Extract the data that will be saved.
if (object instanceof WebAuthenticationDetails) {
WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object;
results.put("remoteAddress", authenticationDetails.getRemoteAddress());
results.put("sessionId", authenticationDetails.getSessionId());
} else if (object != null) {
results.put(key, object.toString());
} else {
results.put(key, "null");
}
}
}
return results;
}
示例9: getRemoteAddress
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Optional<String> getRemoteAddress() {
return getDetails().flatMap(details -> {
String address;
if (details instanceof OAuth2AuthenticationDetails) {
address = OAuth2AuthenticationDetails.class.cast(details).getRemoteAddress();
} else if (details instanceof WebAuthenticationDetails) {
address = WebAuthenticationDetails.class.cast(details).getRemoteAddress();
} else {
throw new IllegalStateException("Unsupported auth details type " + details.getClass());
}
return Optional.ofNullable(address);
});
}
示例10: getSessionId
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Optional<String> getSessionId() {
return getDetails().flatMap(details -> {
String sessionId;
if (details instanceof OAuth2AuthenticationDetails) {
sessionId = OAuth2AuthenticationDetails.class.cast(details).getSessionId();
} else if (details instanceof WebAuthenticationDetails) {
sessionId = WebAuthenticationDetails.class.cast(details).getSessionId();
} else {
throw new IllegalStateException("Unsupported auth details type " + details.getClass());
}
return Optional.ofNullable(sessionId);
});
}
示例11: getDetailsMap
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private Optional<Map<String, Object>> getDetailsMap() {
return getDetails().flatMap(
details -> {
if (details instanceof OAuth2AuthenticationDetails) {
Object decodedDetails = OAuth2AuthenticationDetails.class.cast(
details).getDecodedDetails();
return Optional.ofNullable(Map.class.cast(decodedDetails));
} else if (details instanceof WebAuthenticationDetails) {
return Optional.empty();
} else {
throw new IllegalStateException("Unsupported auth details type "
+ details.getClass());
}
});
}
示例12: getCurrentUserIp
import org.springframework.security.web.authentication.WebAuthenticationDetails; //导入依赖的package包/类
/**
* 取得当前用户登录IP, 如果当前用户未登录则返回空字符串.
*/
public static String getCurrentUserIp() {
Authentication authentication = getAuthentication();
if (authentication == null) {
return "";
}
Object details = authentication.getDetails();
if (!(details instanceof WebAuthenticationDetails)) {
return "";
}
WebAuthenticationDetails webDetails = (WebAuthenticationDetails) details;
return webDetails.getRemoteAddress();
}