添加获取用户id,名称的service实现
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.gzzn.omms.adminapi.dto;
|
||||
|
||||
/**
|
||||
* header 中用户信息封装
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
public class HeaderUserDto {
|
||||
private String userId;
|
||||
private String userName;
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.gzzn.omms.adminapi.exception;
|
||||
|
||||
public class UserNoLoginException extends RuntimeException {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.gzzn.omms.adminapi.service;
|
||||
|
||||
public interface IUserService {
|
||||
String getCurrentUserId();//获取当前用户id
|
||||
String getCurrentUserName();//获取当前用户名
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.gzzn.omms.adminapi.service.impl;
|
||||
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.gzzn.omms.adminapi.dto.HeaderUserDto;
|
||||
import com.gzzn.omms.adminapi.exception.UserNoLoginException;
|
||||
import com.gzzn.omms.adminapi.service.IUserService;
|
||||
|
||||
import sun.misc.BASE64Decoder;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements IUserService {
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Override
|
||||
public String getCurrentUserId() {
|
||||
return this.getHeaderUserDto().getUserId();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getCurrentUserName() {
|
||||
return this.getHeaderUserDto().getUserName();
|
||||
}
|
||||
|
||||
//获取网关header中传递的用户信息
|
||||
private HeaderUserDto getHeaderUserDto() {
|
||||
try
|
||||
{
|
||||
String userStr = request.getHeader("user");
|
||||
if(null == userStr)
|
||||
{
|
||||
throw new UserNoLoginException();
|
||||
}
|
||||
BASE64Decoder base64Decoder = new BASE64Decoder();
|
||||
byte[] bt = base64Decoder.decodeBuffer(userStr);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
return objectMapper.readValue(bt, HeaderUserDto.class);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException("header userinfo error!"+e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user