package*.security;

import java.util.ArrayList;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.WebAuthenticationDetails; import com.opensymphony.xwork2.ActionSupport; import *.security.AutoLoginFilter; public class Login extends ActionSupport { private UserDetailsService userDetailService;
private AuthenticationManager authenticationManager;
public String poc_auto_login_user;
public String poc_auto_login_password;
public String errorMsg;
private static Log log = LogFactory.getLog(AutoLoginFilter.class); public String login(){
String targetUser = getPoc_auto_login_user();
// String targetUserPassword = getPoc_auto_login_password();
if (targetUser != null && targetUser.trim().length() > 0
// && targetUserPassword != null
// && targetUserPassword.trim().length() > 0
) { Authentication oridinalAuth = SecurityContextHolder.getContext()
.getAuthentication();
if (oridinalAuth != null) { Object prin = oridinalAuth.getPrincipal();
String user = null;
if (prin instanceof UserDetails) { user = ((UserDetails) prin).getUsername(); } else {
user = (String) prin;
} if (targetUser.equals(user)) {
this.setErrorMsg(targetUser+"账号已经登录!!");
log.info("尝试自动登录已经登录的账号:" + user + ",自动登录过程已经取消");
//return INPUT;
}
} UserDetails user = null; // 处理自动登录逻辑,这里没有验证密码,如果要验证密码就在这里进行修改
try { user = userDetailService.loadUserByUsername(targetUser); } catch (UsernameNotFoundException e) {
this.setErrorMsg(targetUser+"账号不存在!!");
log.info("由于用户账号不存在,已经取消自动登录:" + targetUser);
//return INPUT;
} // 执行登录,这里没有进行密码比较,直接进行的登录。
// String realPassword = user.getPassword();
if (true//targetUserPassword.equals(realPassword)
) {
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
user, user.getPassword(), new ArrayList(
user.getAuthorities()));
authentication.setDetails(new WebAuthenticationDetails(
(HttpServletRequest) ServletActionContext.getRequest())); authentication = (UsernamePasswordAuthenticationToken) authenticationManager
.authenticate(authentication); SecurityContextHolder.getContext().setAuthentication(
authentication); /*HttpServletRequest req = ServletActionContext.getRequest();
String loginParam = req.getParameter("loginParam").trim();
if(null != loginParam && !loginParam.equals("")){
Cookie cookie = new Cookie("loginParam", loginParam);
cookie.setPath("/");
ServletActionContext.getResponse().addCookie(cookie);
}*/ log.info("已经自动登录账号:" + targetUser);
return SUCCESS;
} else {
this.setErrorMsg(targetUser+"密码错误");
log.info("用户密码错误:" + targetUser);
//return INPUT;
} }else{
this.setErrorMsg("请输入用户名和密码");
} return SUCCESS;
} public String getPoc_auto_login_user() {
return poc_auto_login_user;
} public void setPoc_auto_login_user(String poc_auto_login_user) {
this.poc_auto_login_user = poc_auto_login_user;
} public String getPoc_auto_login_password() {
return poc_auto_login_password;
} public void setPoc_auto_login_password(String poc_auto_login_password) {
this.poc_auto_login_password = poc_auto_login_password;
} public String getErrorMsg() {
return errorMsg;
} public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
} public void setUserDetailService(UserDetailsService userDetailService) {
this.userDetailService = userDetailService;
} public void setAuthenticationManager(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
} }

  

security自动登陆的更多相关文章

  1. Spring Security 自定义 登陆 权限验证

    转载于:https://www.jianshu.com/p/6b8fb59b614b 项目简介 基于Spring Cloud 的项目,Spring Cloud是在Spring Boot上搭建的所以按照 ...

  2. spring security 自动登录 --- 心得

    1.前言 仍然是使用cookie存储登录数据,但是存储的数据 由 spring security自动创建 ,当登出后自动删除cookie, 如果不登出也仍在生命周期内,关闭浏览器再打开将会自动登录,无 ...

  3. 配置putty自动登陆服务器

    putty是一款知名的SSH工具,可以用来登陆linux服务器,提供了终端.SSH是secure Shell的缩写.我之前也有一篇文章介绍这个话题:http://www.cnblogs.com/che ...

  4. shell实现SSH自动登陆

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  5. 在win10中创建开机自动登陆的网络驱动器

    前提环境: win10系统电脑一台. centos系统电脑一台,在该系统中安装samba,并配置共享. 要解决的问题: 在win10做了网络映射,将centos共享的文件夹设置成本地硬盘.在映射时,也 ...

  6. curl模拟自动登陆&采集网页数据

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...

  7. Windows Server 2008 R2怎样设置自动登陆

    Windows Server 2008 R2是一款服务器操作系统,提升了虚拟化.系统管理弹性.网络存取方式,以及信息安全等领域的应用,Windows Server 2008 R2也是第一个只提供64位 ...

  8. Android之记住密码与自动登陆实现

    本文主要讲述了利用sharedpreference实现记住密码与自动登陆功能 根据checkbox的状态存储用户名与密码 将结果保存在自定义的application中,成为全局变量 布局文件 < ...

  9. js实现自动登陆的按钮

    自动按钮,只要实现当移入是提示用户不要在公共地方使用自动登陆 主要用onmouseover函数,本来提示div隐藏,当移入时div显示. <style type="text/css&q ...

随机推荐

  1. Hmtl5 <input>中placeholder属性(新属性)

    Hmtl5 <input>中placeholder属性(新属性) 一.定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示 ...

  2. 20145105 《Java程序设计》实验一总结

    实验一   Java开发环境的熟悉 一.    实验内容: (一)使用JDK编译.运行简单的程序 (二)使用idea编辑.编译.运行.调试Java程序. 二.    实验步骤: (一)   命令行下J ...

  3. [微信开发] - weixin4j关键类解析

    TokenUtil : get()获取我方自定义的token(从配置文件或数据库) checkSignature(Str..... (服务器配置连接验证有效性) /* * 微信公众平台(JAVA) S ...

  4. Spring IOC 源码简单分析 03 - 循环引用

    ### 准备 ## 目标 了解 Spring 如何处理循环引用 ##测试代码 gordon.study.spring.ioc.IOC03_CircularReference.java   ioc03. ...

  5. Android之获取输入用户名与密码发送短信

    打算在过两三天的时间我就要准备出发去浙江了,所以把之前的资料来个总结. 这都是在课堂上做过的作业. 好了,废话少说,直接上代码. 步骤: 1.设置单击事件2.获取输入的QQ号码与密码3.判断输入获取的 ...

  6. Java 集合-Collections工具类

    2017-11-05 23:41:53 Collections类 Collections类:Collections类是针对集合进行操作的工具类,都是静态方法. 常用方法: public static ...

  7. Model/ModelMap 和 ModelAndView 的区别使用

    Model/ModelMap 和 ModelAndView 的区别使用 Model/ModelMap controller: package springmvc.controller; import ...

  8. 使用Zend studio+WAMP来调试Wordpress后台的PHP程序的一些非常关键的信息(原创)

    一.Zend studio代码格式化快捷键:选中代码,Ctrl+Shift+F( 注意,在英文输入发状态下使用!) .Zend studio实用快捷键 :http://www.zendstudio.n ...

  9. js获取当前日期(年月日格式)

    function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var year = date ...

  10. javascript 时间与时间戳的转换

    一:时间转时间戳:javascript获得时间戳的方法有五种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(new ...