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. SQL学习笔记四之MySQL数据操作

    阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操作: DML =========== ...

  2. bzoj1606 / P2925 [USACO08DEC]干草出售Hay For Sale(01背包)

    P2925 [USACO08DEC]干草出售Hay For Sale 简化版01背包(连价值都免了) 直接逆推解决 #include<iostream> #include<cstdi ...

  3. linux第六周

    一.知识概要 进程的描述 进程描述符task_struct数据结构(一)进程描述符task_struct数据结构(二)进程的创建 进程的创建概览及fork一个进程的用户态代码理解进程创建过程复杂代码的 ...

  4. 如何在命令提示符下编译运行含有Package的java文件

    这篇是大二自学Java的时候记下的笔记,中午回顾印象笔记的时候意外看到了这篇.看到多年前写下的文字,我想起那时候我对Java的懵懵懂懂,每天晚上在图书馆照着书写书上的示例代码,为一个中文分号绞尽脑汁, ...

  5. SDN前瞻 软件定义网络的一些概念

    SDN的核心:可编程性 SDN的思想:SOA面向服务 面向服务的体系结构(service-oriented architecture SOA) 使网络连接的大量计算机易于合作,以 服务 而不是人工交互 ...

  6. HTML和CSS美化 登入框的页面

    <div id="leftbody"> <p class="p0">如果您感觉您的网站够炫美可以提交给我们!</p> < ...

  7. 无法安装64位版本的office因为在您的pc

    无法安装64位版本的office因为在您的pcWindows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Installer\Products\00 ...

  8. 001——php字符串中的字符串定义

    <?php /** * 一.定义字符串: * ''单引号 ""双引号 <<<定界符 */ /* $url='http://baidu.com'; $baid ...

  9. How to get the full error stack trace of SharePoint

    博客地址 http://blog.csdn.net/foxdave SharePoint开发,怎么得到真实的详细错误信息. 大家在开发遇到页面报错需要提问的时候,先将详细错误信息获取到再提问,谢谢. ...

  10. 词云:解决pip install wordcloud安装过程中报错“error: command 'x86_64-linux-gnu-gcc' failed with exit status 1”问题

    外部环境:ubuntu16.04, 64bits, 全局环境python2.7 在虚拟环境(python3.5)中执行 pip install wordcloud 时安装失败,报错: error: c ...