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 用 ...
随机推荐
- ios逆向过程中lldb调试技巧
在ios逆向过程中,善于运用lldb,会给逆向带来很大的方便 一般的命令: 1.image list -o -f 看看各个模块在内存中的基址 2.register read r0 读取寄存器r0的 ...
- Python输入函数 raw_input( ) 与 input()
一. raw_input() 在Python中,获取键盘输入的数据的方法是采用 raw_input 函数,那么这个 raw_input 怎么用呢? 注意: raw_input()的小括号中放入的是,提 ...
- 【Linux】Apache Httpd 服务管理
基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐 [user@master1 ~]$ /usr/local ...
- vue.js基础知识篇(5):过渡、Method和Vue实例方法
第8章:过渡 1.CSS过渡 2.JavaScript过渡 3.渐进过渡 第9章:method Vue.js的事件一般通过v-on指令配置在HTML中,虽然也可以在js的代码中使用原生的addEven ...
- static和final修饰方法
static修饰的方法是静态方法,所有的对象共用一份,也就是共享方法.static方法是可以被继承,然后可以被重写和重载. final修饰的方法是不可变方法,final方法所在类被继承时,被final ...
- Why I donot give up cnblogs for Jianshu
我为什么不放弃博客园使用简书 Why I donot give up cnblogs for Jianshu Chapter0 从2016年8月开始接触简书开始,就有些喜欢上简书了,因为简书支持 ma ...
- C# 使用NPOI 实现Excel的简单导入导出
private void btnImport_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt ...
- this和super的运用
这几天看到类在继承时会用到this和super,这里就做了一点总结,与各位共同交流,有错误请各位指正~ this this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针. this ...
- 使用js jquery分别获取地址栏参数值
使用JS获取地址栏参数 方法一: function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +& ...
- css中使用变量
2017年3月,微软宣布 Edge 浏览器将支持 CSS 变量.这个重要的 CSS 新功能,所有主要浏览器已经都支持了. 声明css变量的时候,变量名前面要加两根连词线(--).变量名大小写敏感,-- ...