单点登录

编程

1. 用户登录功能

   用户登录分为两部分:

     (1)先要在拦截器中判断访问url是否为登录页面,否:从request中获取Session,再从session获取绑定用户,若没有,则表示用户没有登录,重定向到登录页面

     (2)处理登录请求:

                  

  UserController

//用户登录

@RequestMapping(value="/login", method=RequestMethod.POST)

@ResponseBody

public TaotaoResult userLogin(String username, String password,

HttpServletRequest request, HttpServletResponse response) {

try {

TaotaoResult result = userService.userLogin(username, password, request, response);

return result;

} catch (Exception e) {

e.printStackTrace();

return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));

}

}

 UserService

	/**

* 用户登录

* <p>Title: userLogin</p>

* <p>Description: </p>

* @param username

* @param password

* @return

* @see com.taotao.sso.service.UserService#userLogin(java.lang.String, java.lang.String)

*/

@Override

public TaotaoResult userLogin(String username, String password,

HttpServletRequest request, HttpServletResponse response) {

TbUserExample example = new TbUserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo(username);

List<TbUser> list = userMapper.selectByExample(example);

//如果没有此用户名

if (null == list || list.size() == 0) {

return TaotaoResult.build(400, "用户名或密码错误");

}

TbUser user = list.get(0);

//比对密码

if (!DigestUtils.md5DigestAsHex(password.getBytes()).equals(user.getPassword())) {

return TaotaoResult.build(400, "用户名或密码错误");

}

//生成token

String token = UUID.randomUUID().toString();

//保存用户之前,把用户对象中的密码清空。

user.setPassword(null);

//把用户信息写入redis

jedisClient.set(REDIS_USER_SESSION_KEY + ":" + token, JsonUtils.objectToJson(user));

//设置session的过期时间

jedisClient.expire(REDIS_USER_SESSION_KEY + ":" + token, SSO_SESSION_EXPIRE);

//添加写cookie的逻辑,cookie的有效期是关闭浏览器就失效。

CookieUtils.setCookie(request, response, "TT_TOKEN", token);

//返回token

return TaotaoResult.ok(token);

}

 

CookieUtils

/**

*

* Cookie 工具类

*

*/

public final class CookieUtils {

/**

* 得到Cookie的值, 不编码

*

* @param request

* @param cookieName

* @return

*/

public static String getCookieValue(HttpServletRequest request, String cookieName) {

return getCookieValue(request, cookieName, false);

}

/**

* 得到Cookie的值,

*

* @param request

* @param cookieName

* @return

*/

public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {

Cookie[] cookieList = request.getCookies();

if (cookieList == null || cookieName == null) {

return null;

}

String retValue = null;

try {

for (int i = 0; i < cookieList.length; i++) {

if (cookieList[i].getName().equals(cookieName)) {

if (isDecoder) {

retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");

} else {

retValue = cookieList[i].getValue();

}

break;

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return retValue;

}

/**

* 得到Cookie的值,

*

* @param request

* @param cookieName

* @return

*/

public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {

Cookie[] cookieList = request.getCookies();

if (cookieList == null || cookieName == null) {

return null;

}

String retValue = null;

try {

for (int i = 0; i < cookieList.length; i++) {

if (cookieList[i].getName().equals(cookieName)) {

retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);

break;

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return retValue;

}

/**

* 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue) {

setCookie(request, response, cookieName, cookieValue, -1);

}

/**

* 设置Cookie的值 在指定时间内生效,但不编码

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, int cookieMaxage) {

setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);

}

/**

* 设置Cookie的值 不设置生效时间,但编码

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, boolean isEncode) {

setCookie(request, response, cookieName, cookieValue, -1, isEncode);

}

/**

* 设置Cookie的值 在指定时间内生效, 编码参数

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, int cookieMaxage, boolean isEncode) {

doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);

}

/**

* 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, int cookieMaxage, String encodeString) {

doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);

}

/**

* 删除Cookie带cookie域名

*/

public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName) {

doSetCookie(request, response, cookieName, "", -1, false);

}

/**

* 设置Cookie的值,并使其在指定时间内生效

*

* @param cookieMaxage cookie生效的最大秒数

*/

private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {

try {

if (cookieValue == null) {

cookieValue = "";

} else if (isEncode) {

cookieValue = URLEncoder.encode(cookieValue, "utf-8");

}

Cookie cookie = new Cookie(cookieName, cookieValue);

if (cookieMaxage > 0)

cookie.setMaxAge(cookieMaxage);

if (null != request) {// 设置域名的cookie

String domainName = getDomainName(request);

System.out.println(domainName);

if (!"localhost".equals(domainName)) {

cookie.setDomain(domainName);

}

}

cookie.setPath("/");

response.addCookie(cookie);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 设置Cookie的值,并使其在指定时间内生效

*

* @param cookieMaxage cookie生效的最大秒数

*/

private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName, String cookieValue, int cookieMaxage, String encodeString) {

try {

if (cookieValue == null) {

cookieValue = "";

} else {

cookieValue = URLEncoder.encode(cookieValue, encodeString);

}

Cookie cookie = new Cookie(cookieName, cookieValue);

if (cookieMaxage > 0)

cookie.setMaxAge(cookieMaxage);

if (null != request) {// 设置域名的cookie

String domainName = getDomainName(request);

System.out.println(domainName);

if (!"localhost".equals(domainName)) {

cookie.setDomain(domainName);

}

}

cookie.setPath("/");

response.addCookie(cookie);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 得到cookie的域名

*/

private static final String getDomainName(HttpServletRequest request) {

String domainName = null;

String serverName = request.getRequestURL().toString();

if (serverName == null || serverName.equals("")) {

domainName = "";

} else {

serverName = serverName.toLowerCase();

serverName = serverName.substring(7);

final int end = serverName.indexOf("/");

serverName = serverName.substring(0, end);

final String[] domains = serverName.split("\.");

int len = domains.length;

if (len > 3) {

// www.xxx.com.cn

domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];

} else if (len <= 3 && len > 1) {

// xxx.com or xxx.cn

domainName = "." + domains[len - 2] + "." + domains[len - 1];

} else {

domainName = serverName;

}

}

if (domainName != null && domainName.indexOf(":") > 0) {

String[] ary = domainName.split("\:");

domainName = ary[0];

}

return domainName;

}

}

 

 

2.  

以上是 单点登录 的全部内容, 来源链接: utcz.com/z/512649.html

回到顶部