说明,因为我们的一个项目B使用spring mvc配置的登陆框架,所以对登陆控制全部交给了spring,导致我们如果想通过另一个项目A登陆到项目B就不太容易,具体是项目A登陆了,我们通过一个连接直接跳转到项目B,

前提,项目A用户名密码和项目B的用户名密码必须是一样的

难点:1.项目A用密文登陆,即前端JS对密码加密后传递给后天,但是项目B是明文直接传递给spring框架

思路:我开始通过debug往spring的代码里面寻求突破点,但是找了好长时间,发现跳转太多,不好找。后来,仔细想想,spring能通过什么来保存数据呢,当然是session啦

于是,就有下面的代码:

Set keySet = (Set)session.keySet();
Object val = null;
SecurityContextImpl securityContext = null;
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = null;
for(Object key : keySet){
val = session.get(key);
if(val != null){
securityContext = (SecurityContextImpl) val; //1. SecurityContextImpl
System.out.println(securityContext.getAuthentication());
usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken) securityContext.getAuthentication();//2. UsernamePasswordAuthenticationToken System.out.println(JSONUtils.toJSONString(usernamePasswordAuthenticationToken));
String credentials = usernamePasswordAuthenticationToken.getCredentials()+""; //3.Credentials
if(credentials != null){
System.out.println("credentials============"+credentials);//通过直接打印可以发现该对象对应的实体
System.out.println(credentials);
}
Object principal = usernamePasswordAuthenticationToken.getPrincipal();
if(principal != null){
System.out.println("principal============"+principal);//通过直接打印可以发现该对象对应的实体
System.out.println(JSONUtils.toJSONString(principal));//通过json输出可以查看该对象中的具体属性值
}
List<GrantedAuthority> grantedAuthorities = (List<GrantedAuthority>)usernamePasswordAuthenticationToken.getAuthorities();
if(grantedAuthorities != null){
System.out.println("List<GrantedAuthority>============"+grantedAuthorities);//通过直接打印可以发现该对象对应的实体
System.out.println(JSONUtils.toJSONString(grantedAuthorities));
}
}
}

通过查看所有的get方法,找到所有内部实体,然后在新模拟的登陆方法中生成这些对象

package com.rainsoft.services.index.action;

import com.rainsoft.common.utils.JSONUtils;
import com.rainsoft.framework.web.struts2.SimpleActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class ThirdLoginAction extends SimpleActionSupport { private static final Log logger = LogFactory.getLog(ThirdLoginAction.class); @Resource
private DaoAuthenticationProvider daoAuthenticationProvider;
@Resource
private UserDetailsService userDetailServiceImpl; private SecurityContextImpl securityContextImpl;
/**
*
* 方法功能说明:第三方登陆接口
* @throws IOException
* @date 2017/01/06 10:54:54
*/
public void login() throws Exception {
String userName = request.getParameter("username");
String password = request.getParameter("password");
/*Users user = new Users();
user.setName(userName);
user.setPassword(password);*/ //认证信息,即明文密码,
String credentials = password; //3.Credentials
if(credentials != null){
System.out.println("credentials============");
System.out.println(credentials);
}
// //用户对象 principal============com.rainsoft.framework.entity.Users@a8833b7b
UserDetails principal = userDetailServiceImpl.loadUserByUsername(userName);
if(principal != null){
System.out.println("principal============");
System.out.println(JSONUtils.toJSONString(principal));
}
//用户角色
List<GrantedAuthority> grantedAuthorities =new ArrayList<GrantedAuthority>();
if(grantedAuthorities != null){
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_USERS");
grantedAuthorities.add(grantedAuthority);
} UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(principal,password,grantedAuthorities);
securityContextImpl = (SecurityContextImpl) SecurityContextHolder.getContext();
securityContextImpl.setAuthentication(usernamePasswordAuthenticationToken); session.put("SPRING_SECURITY_CONTEXT",securityContextImpl); response.sendRedirect(request.getContextPath()+"/main!fmpg.do"); }
}

通过上面的代码,我们就可以直接进入系统B,然后操作相应的菜单,以上只是我的个人理解,如果路过的童鞋们有更好的方法,希望不吝赐教

模拟springmvc 内部登陆,跳过spring filter的更多相关文章

  1. spring filter过滤器

    1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...

  2. SpringMVC从Controller跳转到另一个Controller(转)

    http://blog.csdn.net/jackpk/article/details/44117603 [PK亲测] 能正常跳转的写法如下: return "forward:aaaa/bb ...

  3. springMVC controller间跳转 重定向 传递参数的方法

    springMVC controller间跳转 重定向 传递参数的方法 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参 ...

  4. [PHP自动化-进阶]004.Snoopy VS CURL 模拟Discuz.net登陆

    引言:采集论坛第一步就是要模拟登陆,由于各个站点登录表单各不相同,验证方式又是多种多样,所以直接提交用户名密码到登录页面就比较繁琐. 所以我们采用cookie来模拟登陆无疑是最佳捷径. 今天我们要处理 ...

  5. SpringMVC实现客户端跳转

    之前无论是/index跳转到index.jsp 还是/addProduct 跳转到showProduct.jsp,都是服务端跳转. 这一篇练习如何进行客户端跳转 @ 目录 修改IndexControl ...

  6. web应用怎么跳过某些Filter

    在做的项目需要用到cas,而使用cas的话,需要在client的webapp的web.xml中配置好多个filter,但是需要兼容到老的逻辑,如果满足某些条件,就不走cas的filter,满足另外一些 ...

  7. [模拟回调] demo1模拟用字符串调用js函数 demo2模拟springmvc controller回调页面js函数

    demo1. 模拟用字符串调用js 函数 function dataQuery() { var strFun = "testCallBack"; var strParam = &q ...

  8. Spring Filter过滤表单中的非法字符

    使用Spring Filter过滤表单中的非法字符 1 package test.filter; 2 3 import java.io.IOException; 4 import java.util. ...

  9. Ajax登陆,使用Spring Security缓存跳转到登陆前的链接

    Spring Security缓存的应用之登陆后跳转到登录前源地址 什么意思? 用户访问网站,打开了一个链接:(origin url)起源链接 请求发送给服务器,服务器判断用户请求了受保护的资源. 由 ...

随机推荐

  1. 【动态规划】XMU 1583 Sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1583 题目大意: T组数据,对于n(n<=6000)给定序列Xn(Xn<= ...

  2. 编写高质量js代码

    原文链接:http://code.tutsplus.com/tutorials/24-javascript-best-practices-for-beginners--net-5399 jquery代 ...

  3. delphi 通过控件的handle取得控件

    例子代码如下: vartsg:TstringGrid;begintsg:=Tstringgrid(FindControl(handle));//正常使用TstringGrid//tsg......./ ...

  4. 64位CentOS安装32位开发环境编译Nachos

    参考http://stackoverflow.com/questions/7412548/gnu-stubs-32-h-no-such-file-or-directory 1.On CentOS 5. ...

  5. ObsoleteAttribute 可适用于除程序集、模块、参数或返回值以外的所有程序元素。 将元素标记为过时可以通知用户:该元素在产品的未来版本中将被移除。

    官方文档:https://msdn.microsoft.com/zh-cn/library/system.obsoleteattribute(v=vs.110).aspx 备注 ObsoleteAtt ...

  6. Robot Framework selenium2library 常用关键字

    Selenium Library SeleniumLibrary is a Robot Framework test library that uses the popular Selenium we ...

  7. C - A Simple Problem with Integers - poj 3468(区间更新)

    题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...

  8. [置顶] Hibernate运行机理

    Hibernate 的缓存体系 一级缓存: Session 有一个内置的缓存,其中存放了被当前工作单元加载的对象. 每个Session 都有自己独立的缓存,且只能被当前工作单元访问. 二级缓存: Se ...

  9. wxPython学习笔记(初识)

    今天正式开始学习wxPython,基于对类的不熟悉,理解有点生硬,但还是做了些笔记. 1.是什么组成了一个wxpython程序? 一个wxpython程序必须有一个application(wx.App ...

  10. Android学习之电话拨号器

    本人自己是做android驱动的,也会接触到系统层.上层的应用,所以在闲暇的时候也就开始了学习android应用的路程,在这里把这些东西记下来,希望自己能坚持下去,也好以后复习用. 今天先实现一个简单 ...