security自动登陆
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自动登陆的更多相关文章
- Spring Security 自定义 登陆 权限验证
转载于:https://www.jianshu.com/p/6b8fb59b614b 项目简介 基于Spring Cloud 的项目,Spring Cloud是在Spring Boot上搭建的所以按照 ...
- spring security 自动登录 --- 心得
1.前言 仍然是使用cookie存储登录数据,但是存储的数据 由 spring security自动创建 ,当登出后自动删除cookie, 如果不登出也仍在生命周期内,关闭浏览器再打开将会自动登录,无 ...
- 配置putty自动登陆服务器
putty是一款知名的SSH工具,可以用来登陆linux服务器,提供了终端.SSH是secure Shell的缩写.我之前也有一篇文章介绍这个话题:http://www.cnblogs.com/che ...
- shell实现SSH自动登陆
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- 在win10中创建开机自动登陆的网络驱动器
前提环境: win10系统电脑一台. centos系统电脑一台,在该系统中安装samba,并配置共享. 要解决的问题: 在win10做了网络映射,将centos共享的文件夹设置成本地硬盘.在映射时,也 ...
- curl模拟自动登陆&采集网页数据
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- Windows Server 2008 R2怎样设置自动登陆
Windows Server 2008 R2是一款服务器操作系统,提升了虚拟化.系统管理弹性.网络存取方式,以及信息安全等领域的应用,Windows Server 2008 R2也是第一个只提供64位 ...
- Android之记住密码与自动登陆实现
本文主要讲述了利用sharedpreference实现记住密码与自动登陆功能 根据checkbox的状态存储用户名与密码 将结果保存在自定义的application中,成为全局变量 布局文件 < ...
- js实现自动登陆的按钮
自动按钮,只要实现当移入是提示用户不要在公共地方使用自动登陆 主要用onmouseover函数,本来提示div隐藏,当移入时div显示. <style type="text/css&q ...
随机推荐
- Python面试题之Python反射详解
0x00 前言 反射,可以理解为利用字符串的形式去对象中操作成员属性和方法 反射的这点特性让我联想到了exec函数,也是把利用字符串的形式去让Python解释器去执行命令 Python Version ...
- presto-cli通过hive查询hdfs
1. 启动hive metastore 2. 启动hive thrift接口 参考:http://www.cnblogs.com/kisf/p/7497261.html 3. 下载presto se ...
- 2018-2019-1 20189215《Linux内核原理与分析》第二周作业
本周学习了<庖丁解牛>第1章,以及<Linux内核设计与实现>第1.2.18章.通过视频和实验,学会了反汇编一个简单的C程序,也学习了Linux内核调试的一些小技巧和print ...
- 20172305 2018-2019-1 《Java软件结构与数据结构》第七周学习总结
20172305 2018-2019-1 <Java软件结构与数据结构>第七周学习总结 教材学习内容总结 本周内容主要为书第十一章内容: 二叉查找树(附加属性的二叉树) 二叉查找树是对树中 ...
- xlrd基本操作
#coding=utf-8import xlrd def read_excel(): workbook = xlrd.open_workbook('people.xlsx') sheet2 = wor ...
- LA 2963 超级传输(扫描)
https://vjudge.net/problem/UVALive-2963 题意:需要在n个星球上各装一个广播装置,作用范围均为R.每个星球广播A类节目或者B类节目.a表示星球i收听到的和自己广播 ...
- Android SDK manager 闪退
在SDK安装目录找到tools文件夹,点击进入找到android.bat右键编辑:找到如下指令: rem Check we have a valid Java.exe in the path. set ...
- Java中的组合与聚合
组合和聚合是有很大区别的,这个区别不是在形式上,而是在本质上:比如A类中包含B类的一个引用b,当A类的一个对象消亡时,b这个引用所指向的对象也同时消亡(没有任何一个引用指向它,成了垃圾对象),这种情况 ...
- python 随机整数
# Program to generate a random number between and # import the random module import random print(ran ...
- poj1087最大流拆点
刚开始看这题太长了就放着,后来做了之后才发现并不难,就是构造图有点麻烦 一开始写了180行@.@结果tle了,后来想到用map直接访问的话可能会快点,就不用每次循环了 #include<map& ...