本文整理汇总了Java中org.springframework.security.core.context.SecurityContextHolder.getContext方法的典型用法代码示例。如果您正苦于以下问题:Java SecurityContextHolder.getContext方法的具体用法?Java SecurityContextHolder.getContext怎么用?Java SecurityContextHolder.getContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.core.context.SecurityContextHolder
的用法示例。
在下文中一共展示了SecurityContextHolder.getContext方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCurrentUser
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's
* {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our
* application Spring Security usernames are email addresses).
*/
@Override
public CalendarUser getCurrentUser() {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (authentication == null) {
return null;
}
CalendarUser user = (CalendarUser) authentication.getPrincipal();
String email = user.getEmail(); if (email == null) {
return null;
}
CalendarUser result = calendarService.findUserByEmail(email);
if (result == null) {
throw new IllegalStateException(
"Spring Security is not in synch with CalendarUsers. Could not find user with email " + email);
}
logger.info("CalendarUser: {}", result);
return result;
}
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:26,代码来源:SpringSecurityUserContext.java
示例2: getCurrentUser
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's
* {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our
* application Spring Security usernames are email addresses).
*/
@Override
public CalendarUser getCurrentUser() {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (authentication == null) {
return null;
}
User user = (User)authentication.getPrincipal();
String email = user.getUsername();
// String email = user.getEmail();
if (email == null) {
return null;
}
CalendarUser result = calendarService.findUserByEmail(email);
if (result == null) {
throw new IllegalStateException(
"Spring Security is not in synch with CalendarUsers. Could not find user with email " + email);
}
return result;
}
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:27,代码来源:SpringSecurityUserContext.java
示例3: getCurrentUser
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's
* {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our
* application Spring Security usernames are email addresses).
*/
@Override
public CalendarUser getCurrentUser() {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (authentication == null) {
return null;
}
CalendarUser user = (CalendarUser) authentication.getPrincipal();
String email = user.getEmail();
if (email == null) {
return null;
}
CalendarUser result = calendarService.findUserByEmail(email);
if (result == null) {
throw new IllegalStateException(
"Spring Security is not in synch with CalendarUsers. Could not find user with email " + email);
}
logger.info("CalendarUser: {}", result);
return result;
}
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:28,代码来源:SpringSecurityUserContext.java
示例4: getCurrentUserLogin
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Get the login of the current user.
*
* @return the login of the current user
*/
public static String getCurrentUserLogin() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String userName = null;
if (authentication != null) {
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
userName = springSecurityUser.getUsername();
} else if (authentication.getPrincipal() instanceof String) {
userName = (String) authentication.getPrincipal();
}
}
return userName;
}
示例5: isCurrentUserInRole
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* If the current user has a specific authority (security role).
*
* <p>The name of this method comes from the isUserInRole() method in the Servlet API</p>
*
* @param authority the authority to check
* @return true if the current user has the authority, false otherwise
*/
public static boolean isCurrentUserInRole(String authority) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null) {
return authentication.getAuthorities().stream()
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(authority));
}
return false;
}
示例6: getUserId
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Gets the user name/ID from thread local
* @param providedUserId the user id provided by through createBy or updateBy
* @return the user id
*/
private String getUserId(final String providedUserId) {
if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
return SecurityContextHolder.getContext().getAuthentication().getName();
}
return providedUserId;
}
示例7: isAuthenticated
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Check if a user is authenticated.
*
* @return true if the user is authenticated, false otherwise
*/
public static boolean isAuthenticated() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null) {
return authentication.getAuthorities().stream()
.noneMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(AuthoritiesConstants.ANONYMOUS));
}
return false;
}
示例8: getCurrentUser
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's
* {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our
* application Spring Security usernames are email addresses).
*/
@Override
public CalendarUser getCurrentUser() {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (authentication == null) {
return null;
}
String email = null;
if(authentication.getPrincipal() instanceof CalendarUser){
logger.debug("Principal is CalendarUser: [{}]", authentication.getPrincipal());
CalendarUser user = (CalendarUser) authentication.getPrincipal();
email = user.getEmail();
}
else if(authentication.getPrincipal() instanceof String){
logger.debug("Principal is String: [{}]", authentication.getPrincipal());
email = (String) authentication.getPrincipal();
}
else {
logger.debug("Principal is NOT CalendarUser / String: [{}]", authentication.getPrincipal());
email = (String) authentication.getPrincipal();
}
if (email == null) {
return null;
}
CalendarUser result = calendarService.findUserByEmail(email);
if (result == null) {
throw new IllegalStateException(
"Spring Security is not in synch with CalendarUsers. Could not find user with email " + email);
}
logger.info("CalendarUser: {}", result);
return result;
}
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:43,代码来源:SpringSecurityUserContext.java
示例9: checkUpdatePermission
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
private void checkUpdatePermission(Person person) {
if (SecurityContextHolder.getContext() == null) {
throw new UnauthorizedClientException("Unauthorized");
}
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
BrowserUser user = (BrowserUser) auth.getPrincipal();
if ((!person.getId().equals(user.getPerson().getId()) || person.getRole().equals(PersonRole.ROLE_ADMIN))
&& !user.getAuthorities().contains(new SimpleGrantedAuthority(PersonRole.ROLE_ADMIN.name()))) {
throw new UnauthorizedClientException("Only admin can do this");
}
}
示例10: isAuthenticated
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Check if a user is authenticated.
*
* @return true if the user is authenticated, false otherwise
*/
public static boolean isAuthenticated() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Collection<? extends GrantedAuthority> authorities = securityContext.getAuthentication().getAuthorities();
if (authorities != null) {
for (GrantedAuthority authority : authorities) {
if (authority.getAuthority().equals(AuthoritiesConstants.ANONYMOUS)) {
return false;
}
}
}
return true;
}
示例11: getAuthentication
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* 取得Authentication, 如当前SecurityContext为空时返回null.
*/
public static Authentication getAuthentication() {
SecurityContext context = SecurityContextHolder.getContext();
return context.getAuthentication();
}