本文整理汇总了Java中org.springframework.security.core.userdetails.UserDetails.getUsername方法的典型用法代码示例。如果您正苦于以下问题:Java UserDetails.getUsername方法的具体用法?Java UserDetails.getUsername怎么用?Java UserDetails.getUsername使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.core.userdetails.UserDetails
的用法示例。
在下文中一共展示了UserDetails.getUsername方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: targetUrl
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
protected String targetUrl(Authentication authentication) {
UserDetails p = (UserDetails )authentication.getPrincipal();
String username = p.getUsername();
String password = p.getPassword();
String url = "";
Collection<? extends GrantedAuthority> authorities = p.getAuthorities();
List<String> roles = new ArrayList<String>();
for (GrantedAuthority a : authorities) {
roles.add(a.getAuthority());
}
System.out.println("logout handler" + roles);
if (isUser(roles)) {
url = "/after_logout.html?message="+"Thank your, "+ username +" with password " + password +" and role(s): " + roles;
} else if (isAdmin(roles)){
url = "/after_logout.html?message="+"Thank your, "+ username +" with password " + password +" and role(s): " + roles;
} else if (isHrAdmin(roles)){
url = "/after_logout.html?message="+"Thank your, "+ username +" with password " + password +" and role(s): " + roles;
} else{
url = "/after_logout.html?message="+"Thank you, friend!";
}
return url;
}
示例2: handleMessage
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
/**
* 消息处理,js客户端send()的消息会到这里
* 不用
*/
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
if(message.getPayloadLength()==0)return;
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = userDetails.getUsername();
UserInfo userInfo = userInfoDao.getUserInfoByUsername(username);
int userId = userInfo.getUserId();
Message msg=new Gson().fromJson(message.getPayload().toString(),Message.class);
msg.setFrom(userId);
msg.setFromName(username);
msg.setDate(new Date());
sendMessageToGroup(userId, msg.getNotebookId(), new TextMessage(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(msg)));
}
示例3: getGroupChat
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
@RequestMapping("/getGroupChat")
@ResponseBody
public String getGroupChat(@RequestParam("notebookId") int notebookId, @RequestParam("lastChat") int lastChat) {
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = userDetails.getUsername();
UserInfo userInfo = userBasicService.getUserInfoByUsername(username);
int userId = userInfo.getUserId();
int length = cooperateService.getGroupChat(notebookId).size();
if(lastChat == -1) lastChat = length - 1;
String resultList = cooperateService.getGroupChatChunk(notebookId, lastChat);
lastChat = lastChat >= 10 ? lastChat - 10 : -2;
JsonObject json = new JsonObject();
json.addProperty("result", resultList);
json.addProperty("currentUser", userId);
json.addProperty("lastChat", lastChat);
return json.toString();
}
示例4: getCurrentUserLogin
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的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: beforeHandshake
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
System.out.println("Before Handshake");
//get userId
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = userDetails.getUsername();
UserInfo userInfo = userInfoDao.getUserInfoByUsername(username);
int userId = userInfo.getUserId();
//put into websocketSession
attributes.put("userId", userId);
return true;
}
示例6: getCurrentUserLogin
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
/**
* Get 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;
}
示例7: sendMsg
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
@RequestMapping("/sendMsg")
@ResponseBody
public String sendMsg(@RequestParam("notebookId") int notebookId, @RequestParam("text") String text) {
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = userDetails.getUsername();
UserInfo userInfo = userBasicService.getUserInfoByUsername(username);
int userId = userInfo.getUserId();
cooperateService.sendGroupChat(userId, notebookId, new Date(), text);
JsonObject json = new JsonObject();
json.addProperty("result", "success");
json.addProperty("sender", username);
return json.toString();
}
示例8: User
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
public User(UserDetails userDetails){
this(userDetails.getUsername(),userDetails.getPassword());
}
示例9: getUserId
import org.springframework.security.core.userdetails.UserDetails; //导入方法依赖的package包/类
private int getUserId() {
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = userDetails.getUsername();
UserInfo userInfo = userBasicService.getUserInfoByUsername(username);
return userInfo.getUserId();
}