本文整理汇总了Java中org.springframework.security.core.Authentication.getPrincipal方法的典型用法代码示例。如果您正苦于以下问题:Java Authentication.getPrincipal方法的具体用法?Java Authentication.getPrincipal怎么用?Java Authentication.getPrincipal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.core.Authentication
的用法示例。
在下文中一共展示了Authentication.getPrincipal方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCurrentUser
import org.springframework.security.core.Authentication; //导入方法依赖的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
示例2: getCurrentUser
import org.springframework.security.core.Authentication; //导入方法依赖的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
示例3: getCurrentUser
import org.springframework.security.core.Authentication; //导入方法依赖的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
示例4: updateDashboard
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public DashboardDTO updateDashboard(String dashboardName, DashboardDTO updatedDashboard) {
Dashboard currentDashboard = this.getRepositoryDashboard(dashboardName);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String authUser = "anonymous";
if(auth != null) {
authUser = (String) auth.getPrincipal();
canEdit(authUser, currentDashboard);
}
if (updatedDashboard.getAdminUsers() == null) {
updatedDashboard.setAdminUsers(Arrays.asList(authUser));
} else if (!updatedDashboard.getAdminUsers().contains(authUser)) {
updatedDashboard.getAdminUsers().add(authUser);
}
Dashboard toSave = mergeDashboard(currentDashboard, map(updatedDashboard), authUser);
Dashboard saved = dashboardRepository.save(toSave);
eventService.saveEvent(saved, EventType.DETAIL);
return map(saved);
}
示例5: get
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
/**
* @param authentication is the {@link Authentication} where to retrieve the user from.
* @return the {@link UserData} of the logged in user from the given {@link Authentication}.
*/
public static UserData get(Authentication authentication) {
if (authentication == null) {
throw new IllegalStateException("Authentication not available!");
}
Object principal = authentication.getPrincipal();
if (principal == null) {
throw new IllegalStateException("Principal not available!");
}
try {
return (UserData) principal;
} catch (ClassCastException e) {
throw new IllegalStateException("Principal (" + principal + ") is not an instance of UserData!", e);
}
}
示例6: decide
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public void decide(Authentication authentication, Object o, Collection<ConfigAttribute> collection)
throws AccessDeniedException, InsufficientAuthenticationException {
if (collection == null) {
return;
}
String needRole;
//遍历需要的角色,如果一样,则通过
CustomerUserDetail userDetail = (CustomerUserDetail) authentication.getPrincipal();
List<Role> userRoleList = securityService.getUserRoleList(userDetail.getUsername(), userDetail.getAccountType());
for (ConfigAttribute configAttribute : collection) {
needRole = configAttribute.getAttribute();
for (Role role : userRoleList) {
if (needRole.equals(role.getRoleCode())) {
return;
}
}
}
throw new AccessDeniedException("Cannot Access!");
}
示例7: convertUserAuthentication
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public Map<String, ?> convertUserAuthentication(Authentication userAuthentication) {
Map<String,Object> tokenContent = new HashMap<>();
CommonUser principal = (CommonUser) userAuthentication.getPrincipal();
User user = principal.getUser();
tokenContent.put("iss", "http://localhost:8090");
tokenContent.put("aud", "testclient");
tokenContent.put("sub", user.getId());
tokenContent.put("given_name", user.getFirstname());
tokenContent.put("family_name", user.getLastname());
tokenContent.put("email", user.getEmail());
tokenContent.put("roles", user.getRoles().stream().map(Role::getName).collect(Collectors.toList()));
return tokenContent;
}
示例8: onAuthenticationSuccess
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response,
Authentication authentication)
throws IOException,ServletException {
//记录登录成功日志
CustomerUserDetail userDetails = (CustomerUserDetail)authentication.getPrincipal();
LoginLog loginLog = new LoginLog();
loginLog.setId(IdWorker.getLongId());
loginLog.setUserAccount(userDetails.getUsername());
loginLog.setUserId(userDetails.getId());
loginLog.setSystemCode(SystemConstant.OOS_SYSTEM);
loginLog.setSysAddTime(new Date());
loginLog.setSysAddUser(userDetails.getId());
loginLogMapper.insertSelective(loginLog);
logger.debug("用户[ID:" + userDetails.getId() + ", account:" + userDetails.getUsername() + "]于" + DateUtil.getCurrDateTime() + "登录");
super.onAuthenticationSuccess(request, response, authentication);
}
示例9: decide
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
public void decide(Authentication authentication, Object o, Collection<ConfigAttribute> collection)
throws AccessDeniedException, InsufficientAuthenticationException {
if (collection == null) {
return;
}
String needRole;
//遍历需要的角色,如果一样,则通过,避免角色信息变了,从数据库取
CustomerUserDetail userDetail = (CustomerUserDetail) authentication.getPrincipal();
List<Role> roleList = securityService.getUserRoleList(userDetail.getUsername(), userDetail.getAccountType());
for (ConfigAttribute configAttribute : collection) {
needRole = configAttribute.getAttribute();
for (Role aRoleList : roleList) {
if (aRoleList != null && needRole.equals(aRoleList.getRoleCode())) {
return;
}
}
}
throw new AccessDeniedException("Cannot Access!");
}
示例10: getCurrentUserLogin
import org.springframework.security.core.Authentication; //导入方法依赖的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;
}
示例11: getLoggedUserDetails
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
public Optional<TwoFaUserDetails> getLoggedUserDetails(){
SecurityContext context = SecurityContextHolder.getContext();
Authentication auth = context.getAuthentication();
if (auth != null && auth.getPrincipal() instanceof TwoFaUserDetails){
return Optional.of( ((TwoFaUserDetails) auth.getPrincipal() ));
}
return Optional.empty();
}
示例12: commentList
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@RequestMapping("/user/commentList")
public String commentList(PageAndSorted page, Model model, Authentication auth) {
if (auth == null) {
return "redirect:/login";
}
TzUserDetails user = (TzUserDetails) auth.getPrincipal();
List<ShopProductComment> comments = this.shopProductCommentService.findContentByUserId(user.getUid(), page);
int count = this.shopProductCommentService.countContentByUserId(user.getUid());
page.setCount(count);
model.addAttribute("comments", comments);
model.addAttribute("page", page);
return "personal/personal_comment";
}
示例13: getUser
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
private UserContext getUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.isAuthenticated() && authentication.getPrincipal() instanceof UserContext)
return (UserContext)authentication.getPrincipal();
return null;
}
示例14: authenticate
import org.springframework.security.core.Authentication; //导入方法依赖的package包/类
@Override
@Transactional (propagation=Propagation.REQUIRED)
public Authentication authenticate (Authentication authentication)
throws AuthenticationException
{
String username = (String) authentication.getPrincipal ();
String password = (String) authentication.getCredentials ();
String ip = "unknown";
String proxy = null;
if (authentication.getDetails () instanceof WebAuthenticationDetails)
{
ip = ((WebAuthenticationDetails)authentication.getDetails ())
.getRemoteAddress ();
}
if (authentication.getDetails() instanceof ProxyWebAuthenticationDetails)
{
String proxyAddress = ((ProxyWebAuthenticationDetails) authentication.getDetails()).getProxyAddress();
if (proxyAddress != null)
{
proxy = " (proxy: " + proxyAddress + ")";
}
}
LOGGER.info("Connection attempted by '{}' from {}",
authentication.getName(), (proxy != null ? ip + proxy : ip));
User user = userService.getUserNoCheck (username);
if (user == null || user.isDeleted ())
{
throw new BadCredentialsException (errorMessage);
}
PasswordEncryption encryption = user.getPasswordEncryption ();
if ( !encryption.equals (PasswordEncryption.NONE))
{
MessageDigest md;
try
{
md = MessageDigest.getInstance (encryption.getAlgorithmKey ());
password =
new String (
Hex.encode (md.digest (password.getBytes ("UTF-8"))));
}
catch (NoSuchAlgorithmException | UnsupportedEncodingException e)
{
throw new BadCredentialsException ("Authentication process failed",
e);
}
}
if ( !user.getPassword ().equals (password))
{
LOGGER.warn (
new Message (MessageType.USER, "Connection refused for '" +
username
+ "' from " + ip +
" : error in login/password combination"));
throw new BadCredentialsException (errorMessage);
}
for (AccessRestriction restriction : user.getRestrictions ())
{
LOGGER.warn ("Connection refused for '" + username +
"' from " + ip + " : account is locked (" +
restriction.getBlockingReason () + ")");
throw new LockedException (restriction.getBlockingReason ());
}
LOGGER.info ("Connection success for '" + username + "' from " + ip);
return new ValidityAuthentication (user, user.getAuthorities ());
}