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 ...
随机推荐
- hbase(三)coprocessor
介绍 coprocessor这个单词看起来很神秘,直译为协处理器,其实可以理解成依赖于regionserver进程的辅助处理接口. hbae在0.92版本之后提供了coprocessor接口.目前hb ...
- Mac下安装hexo Error: Cannot find module './build/Release/DTraceProviderBindings 解决
参考: Github:Mac 下已经装了hexo,仍旧报错 官方文档 $ npm install hexo --no-optional if it doesn't work try $ npm uni ...
- BZOJ 3123 【SDOI2013】 森林
题目链接:森林 这道题想法很显然.既然只有加边而没有删边,那么每次启发式合并就可以了.查询路径\(k\)小似乎需要主席树,那么把主席树和倍增表一起暴力重构就好了. 然后发现这样的空间复杂度是\(O(n ...
- python 字符串压缩
import zlib s = b'witch which has which witches wrist watch' print(len(s)) t = zlib.compress(s) prin ...
- RHEL6.5恢复root密码
1.开机上下键停留在如下界面,键盘输入小写e: 2.选择如下选项,并输入小写e: 3.输入1,回车进入单用户模式: 4.键盘输入小写b,进行启动: 5.进入到单用户模式: 6.修改root用户密码,并 ...
- JSP 语法
JSP 语法 本小节将会简单地介绍一下JSP开发中的基础语法. 脚本程序 脚本程序可以包含任意量的Java语句.变量.方法或表达式,只要它们在脚本语言中是有效的. 脚本程序的语法格式: <% 代 ...
- wpf 中关于Image中样式Style的一点总结
第一种写法: (1):定义样式 <Style x:Key="imgStyle" TargetType="Image"> : <!-- Tar ...
- IOS-线程(GCD)
一.GCD的使用 // // IBController3.m // IBCoder1 // // Created by Bowen on 2018/4/25. // Copyright © 2018年 ...
- MySQL使用和操作总结(《MySQL必知必会》读书笔记)
简介 MySQL是一种DBMS,即它是一种数据库软件.DBMS可分为两类:一类是基于共享文件系统的DBMS,另一类是基于客户机——服务器的DBMS.前者用于桌面用途,通常不用于高端或更关键应用. My ...
- 彻底明白IP地址——计算相关地址
知道ip地址和子网掩码后可以算出: 1. 网络地址 2. 广播地址 3. 地址范围 4. 本网有几台主机 例1:下面例子IP地址为192·168·100·5 子网掩码是255·255·255·0.算出 ...