我们现在开放一个链接给其他系统,来访问我们的系统

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加密的更多相关文章

  1. ASP.NET会员注册登录模块(MD5加密,Parameters防止SQL注入,判断是否注册)

    MD5加密,Parameters防止SQL注入: protected void btnLog_Click(object sender, EventArgs e)     {         //获取验 ...

  2. nodejs 用户登录密码md5加密

    jade文件 div.login ul.inp-content  li span= '用户名:' input.ui-input1#input1(placeholder='请输入手机号')  li sp ...

  3. 登录之md5加密

    语句: password = hex_md5(password); 引入js文件: md5.js: /* * A JavaScript implementation of the RSA Data S ...

  4. 系统开发中使用拦截器校验是否登录并使用MD5对用户登录密码进行加密

    项目名称:客户管理系统 项目描述: 项目基于javaEE平台,B/S模式开发.使用Struts2.Hibernate/Spring进行项目框架搭建.使用Struts中的Action 控制器进行用户访问 ...

  5. Spring-Security (学习记录五)--配置登录时,密码采用md5加密,以及获取登录信息属性监听同步自己想要的登录信息

    目录 1. PasswordEncoder 采用密码加密 2. 获取当前的用户信息 1. PasswordEncoder 采用密码加密 使用前面的例子.可以看出我们数据库密码是采用明文的,我们在登录的 ...

  6. 一个简单的后台与数据库交互的登录与注册[sql注入处理,以及MD5加密]

    一.工具: vs2013[因为我现在用的也是2013,版本随便你自己开心] sql2008[准备过久升级] 二.用到的语言: HTML+CSS+Jquery+Ajax+sqlserver HTML[相 ...

  7. 登录功能(MD5加密)

    登录这个功能,是不管哪个项目都会用到的,登录做的好坏,安全性的保障将直接影响到整个系统的成败,尤其是一些安全性要求比较严格的项目 1.首先需要对密码进行加密,这里用到的是md5加密,需要在login. ...

  8. Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作

    # -*- coding: gb2312 -*- #用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作 #作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.co ...

  9. Apache Shiro(三)-登录认证和权限管理MD5加密

    md5 加密 在前面的例子里,用户密码是明文的,这样是有巨大风险的,一旦泄露,就不好了.所以,通常都会采用非对称加密,什么是非对称呢?就是不可逆的,而 md5 就是这样一个算法.如代码所示 123 用 ...

随机推荐

  1. vue 生命周期

    一 vue的生命周期如下图所示(很清晰) 二 vue生命周期的栗子 注意触发vue的created事件以后,this便指向vue实例,这点很重要 <!DOCTYPE html> <h ...

  2. 2017多校第10场 HDU 6180 Schedule 贪心,multiset

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6180 题意:给了一些任务的开始时间和终止时间,现在让我们安排k台及机器,让这些任务在k太机器上最小,并 ...

  3. 一句话搞定-phpStudy安装yaf扩展

    首先下载phpStudyX64位的,然后傻瓜式安装,安装完下载yaf,由于yaf扩展的网站在国外很难下载,需要FQ,所以我这里下载了yaf5.6nts.zip,解压后把php_yaf.dll这个文件粘 ...

  4. BotVS配置托管者-基于阿里云

    1. 上传Linux 64位 托管者并解压在 https://www.botvs.com/m/add-node 上下载Linux 64位 托管者, 当前下载地址 https://dn-botvs.qb ...

  5. [js高手之路]HTML标签解释成DOM节点

    最近在封装一个开源框架,已经写了500行, 已经具备jquery的大多数常用功能.跟jquery的使用方法完全一样,jquery的选择器,几乎都能支持,为什么说这事,跟这篇文章的主题有毛关系呢?因为这 ...

  6. mapper.xml是怎样实现Dao层接口

    上午写了一个简单的 从xml读取信息实例化一个Bean对象.下午就开始想mybatis是怎么通过xml文件来实现dao层接口的,一开始想直接用Class.forName(String name)然后调 ...

  7. sed武功心法(info sed翻译+注解)

    本文中的提到GNU扩展时,表示该功能是GNU为sed提供的(即GNU版本的sed才有该功能),一般此时都会说明:如果要写具有可移植性的脚本,应尽量避免在脚本中使用该选项. 本文中的正则表达式几乎和gr ...

  8. hibernate的基本配置

    1   Hibernate是一个非侵入式的ORMapping的框架. 2   Hibernate是一个能够将JAVA对象  通过   映射关系    映射到   关系型数据库的  这样一个框架 Hib ...

  9. 配置HAProxy支持https协议

    author:JevonWei 版权声明:原创作品 实现http重定向到https HAProxy 创建CA证书 [root@HAProxy ~]# cd /etc/haproxy/ [root@HA ...

  10. java常见加密方式介绍

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt260 本篇内容简要介绍BASE64.MD5.SHA.HMAC几种加密算法.  ...