SpringSecurity 登录 - 以及Md5加密
我们现在开放一个链接给其他系统,来访问我们的系统
http://localhost:8080/hulk-teller-web/haihui!init.jspa?loginId=teller01&key=SD33OH45O3HJ21O34N34O5
这样的方式登录.
1)按照约定的规则生成key
package hulk.frame.haihui.service;
import hulk.frame.haihui.entity.HaiHuiLogin;
import hulk.frame.haihui.support.Base32;
import hulk.frame.user.service.UserService;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("haihuiService")
public class HaiHuiServiceImpl implements HaiHuiService {
private final static Logger logger = LogManager.getLogger(HaiHuiServiceImpl.class);
// @Autowired
// private PermissionService permissionService;
@Autowired
private UserService userService;
@Override
public boolean checkHaiHuiLogin(HaiHuiLogin loginUser) {
// TODO Auto-generated method stub
if(loginUser!=null){
//
Integer userId=userService.getUserIdByLoginId(loginUser.getLoginId());
if(userId!=null){
// 将用户名设置到海辉用户登录对象中
loginUser.setLoginName(userService.getUserNameByUserId(userId));
// 我们平台生成的Key
String mykey=this.generateKey(loginUser);
if(mykey.equals(loginUser.getKey())){
return true;
}
}
}
return false;
}
private String generateKey(HaiHuiLogin loginUser) {
//规则第一步: loginId + loginName + date 生成
String dateStr=new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
dateStr=dateStr.substring(0, dateStr.length()-1);
String sSource=loginUser.getLoginId()+loginUser.getLoginName()+dateStr;
// 规则第二步:字符串反转
StringBuffer sb=new StringBuffer(sSource);
sSource=sb.reverse().toString();
// 规则第三步:Md5加密
// Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
// return passwordEncoder.encodePassword(sSource, null);
try {
MessageDigest md= MessageDigest.getInstance("MD5");
md.update(sSource.getBytes("UTF-8"));
String digest = Base32.encode(md.digest());
return digest;
} catch (NoSuchAlgorithmException e) {
logger.error(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
}
return null;
}
}
2) 自定义的Base32
package hulk.frame.haihui.support;
public class Base32 {
private static final String base32Chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
private static final int[] base32Lookup = {
0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // '0', '1', '2', '3', '4', '5', '6', '7'
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // '8', '9', ':', ';', '<', '=', '>', '?'
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G'
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W'
0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 'X', 'Y', 'Z', '[', '\', ']', '^', '_'
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g'
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'
0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 'x', 'y', 'z', '{', '|', '}', '~', 'DEL'
};
public static String encode(
final byte[] bytes) {
int i = 0, index = 0, digit = 0;
int currByte, nextByte;
StringBuffer base32 = new StringBuffer((bytes.length + 7) * 8 / 5);
while (i < bytes.length) {
currByte = (bytes[i] >= 0) ? bytes[i] : (bytes[i] + 256); // unsign
/* Is the current digit going to span a byte boundary? */
if (index > 3) {
if ((i + 1) < bytes.length) {
nextByte = (bytes[i + 1] >= 0) ? bytes[i + 1] : (bytes[i + 1] + 256);
} else {
nextByte = 0;
}
digit = currByte & (0xFF >> index);
index = (index + 5) % 8;
digit <<= index;
digit |= nextByte >> (8 - index);
i++;
} else {
digit = (currByte >> (8 - (index + 5))) & 0x1F;
index = (index + 5) % 8;
if (index == 0) {
i++;
}
}
base32.append(base32Chars.charAt(digit));
}
return base32.toString();
}
public static byte[] decode(
final String base32) {
int i, index, lookup, offset, digit;
byte[] bytes = new byte[base32.length() * 5 / 8];
for (i = 0, index = 0, offset = 0; i < base32.length(); i++) {
lookup = base32.charAt(i) - '0';
/* Skip chars outside the lookup table */
if (lookup < 0 || lookup >= base32Lookup.length) {
continue;
}
digit = base32Lookup[lookup];
/* If this digit is not in the table, ignore it */
if (digit == 0xFF) {
continue;
}
if (index <= 3) {
index = (index + 5) % 8;
if (index == 0) {
bytes[offset] |= digit;
offset++;
if (offset >= bytes.length) {
break;
}
} else {
bytes[offset] |= digit << (8 - index);
}
} else {
index = (index + 5) % 8;
bytes[offset] |= (digit >>> index);
offset++;
if (offset >= bytes.length) {
break;
}
bytes[offset] |= digit << (8 - index);
}
}
return bytes;
}
}
3) 我们的框架是ssh的, 系统使用的安全模式是 SpringSecurity
package hulk.frame.haihui.action;
import hulk.frame.action.BaseActionSupport;
import hulk.frame.haihui.entity.HaiHuiLogin;
import hulk.frame.haihui.service.HaiHuiService;
import hulk.frame.security.CurrentUser;
import hulk.frame.security.SecurityManagerSupport;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
@Controller("haihuiAction")
public class HaiHuiAction extends BaseActionSupport {
private static final long serialVersionUID = 1L;
@Autowired
private SecurityManagerSupport securityManager;
@Autowired
protected HaiHuiService haihuiService;
public String init() {
String loginId=request.getParameter("loginId");
String key=request.getParameter("key");
boolean ret=haihuiService.checkHaiHuiLogin(new HaiHuiLogin(loginId,key));
if(ret){
// 处理当前用户
CurrentUser currUser=(CurrentUser)securityManager.loadUserByUsername(loginId);
Authentication auth = new UsernamePasswordAuthenticationToken(currUser,loginId);
SecurityContextHolder.getContext().setAuthentication(auth);
HttpSession session = request.getSession();
session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext()); // 这个非常重要,否则验证后将无法登陆
return SUCCESS;
}else{
return ERROR;
}
}
}
4) 忽略该链接的请求
......
<http pattern="/haihui!init.jspa" security="none"/>
......
5) struts2 的配置
<action name="auto" class="autoAction">
<result name="success">/ext/auto/app.jsp</result>
<result name="teller">/teller/teller_${pageName}.jsp</result>
</action>
<!-- 海辉登录系统 -->
<action name="haihui" class="haihuiAction">
<result name="success">/ext/auto/app.jsp</result>
</action>
SpringSecurity 登录 - 以及Md5加密的更多相关文章
- ASP.NET会员注册登录模块(MD5加密,Parameters防止SQL注入,判断是否注册)
MD5加密,Parameters防止SQL注入: protected void btnLog_Click(object sender, EventArgs e) { //获取验 ...
- nodejs 用户登录密码md5加密
jade文件 div.login ul.inp-content li span= '用户名:' input.ui-input1#input1(placeholder='请输入手机号') li sp ...
- 登录之md5加密
语句: password = hex_md5(password); 引入js文件: md5.js: /* * A JavaScript implementation of the RSA Data S ...
- 系统开发中使用拦截器校验是否登录并使用MD5对用户登录密码进行加密
项目名称:客户管理系统 项目描述: 项目基于javaEE平台,B/S模式开发.使用Struts2.Hibernate/Spring进行项目框架搭建.使用Struts中的Action 控制器进行用户访问 ...
- Spring-Security (学习记录五)--配置登录时,密码采用md5加密,以及获取登录信息属性监听同步自己想要的登录信息
目录 1. PasswordEncoder 采用密码加密 2. 获取当前的用户信息 1. PasswordEncoder 采用密码加密 使用前面的例子.可以看出我们数据库密码是采用明文的,我们在登录的 ...
- 一个简单的后台与数据库交互的登录与注册[sql注入处理,以及MD5加密]
一.工具: vs2013[因为我现在用的也是2013,版本随便你自己开心] sql2008[准备过久升级] 二.用到的语言: HTML+CSS+Jquery+Ajax+sqlserver HTML[相 ...
- 登录功能(MD5加密)
登录这个功能,是不管哪个项目都会用到的,登录做的好坏,安全性的保障将直接影响到整个系统的成败,尤其是一些安全性要求比较严格的项目 1.首先需要对密码进行加密,这里用到的是md5加密,需要在login. ...
- Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作
# -*- coding: gb2312 -*- #用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作 #作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.co ...
- Apache Shiro(三)-登录认证和权限管理MD5加密
md5 加密 在前面的例子里,用户密码是明文的,这样是有巨大风险的,一旦泄露,就不好了.所以,通常都会采用非对称加密,什么是非对称呢?就是不可逆的,而 md5 就是这样一个算法.如代码所示 123 用 ...
随机推荐
- 利用OpenCms9提供的模块创建新站点
OpenCms 9中提供b一个Demo,Demo使用了alkacon的bootstrap模板.如果已经安装了OpenCms 9,可以登陆http://localhost:8080/opencms/op ...
- 【Centos】yum 安装mariaDB
[dream361@za ~]$ sudo yum search mariadb #查找需安装的包 mariadb-libs.x86_64 : The shared libraries require ...
- 【HotSpot】 jps
jps(1) General Commands Manual jps(1) Name jps - Java Virtual Machine Process Status Tool SYNOPSIS j ...
- asp .net 模板引擎 使用 Razor 生成html静态页面
刚开始不是理解 写完之后 觉得还蛮简单的 分为这几个步骤 1.获取页面模板Html 2.获取数据 3.解析模板和数据,生成静态页Html代码 4.生成静态文件 模板形式是mvc的模式,会mvc 看一下 ...
- Struts2-文件上传下载
Struts2文件上传 提供 FileUpload 拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容 fileUpload拦截器 默认在 defaultSta ...
- 关于package.json的理解
在我们打包项目的时候或者使用node的时候,常常会看到package.json这个文件,里面乱七八糟的一大堆json,开始的时候没注意,以为是使用node或者npm的时候自动创建的,后来自己写demo ...
- 数据库面试技巧,通过JDBC展示自己专业性,摘自java web轻量级开发面试教程
这篇文章是我之前写的博文 数据库方面的面试技巧,如何从建表方面展示自己能力 和 面试技巧,如何通过索引说数据库优化能力,内容来自Java web轻量级开发面试教程是一个系列的,通过面试官的视角和大家分 ...
- 【转载】js常用方法和片段
在网上看了不少js方法的总结没,自己也尝试总结过,这篇只迄今为止觉得最清楚的,尤其是call和apply的方法总结,很到位!! 1.javascript删除元素节点 IE中有这样一个方法:remove ...
- window.requestAnimationFrame() ,做逐帧动画,你值得拥有
window.requestAnimationFrame() 方法告诉浏览器您希望执行动画,并请求浏览器调用指定的函数在下一次重绘之前更新动画.该方法使用一个回调函数作为参数,这个回调函数会在浏览器重 ...
- 深度剖析Java变量栈&对象堆
Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变量分配内存空间 ...