IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Spring Security---自定义获取用户IP -> 正文阅读

[网络协议]Spring Security---自定义获取用户IP

之前在Spring Security流程分析的文章里提到过setDetails方法,也就是在WebAuthenticationDetails记录了IP和sessionId。

public WebAuthenticationDetails(HttpServletRequest request) {
   this.remoteAddress = request.getRemoteAddr();
   HttpSession session = request.getSession(false);
   this.sessionId = (session != null) ? session.getId() : null;
}

我们知道,Spring Security 中的Authentication 保存了用户的信息,在Authentication 接口中定义了getDetails()方法,也就是说我们通过这个方法来拿到Details中的信息。

在这里插入图片描述
具体的获取如下:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();

但是这个details中只保存了remoteAddr 和 sessionId 的信息,我们可以自定义WebAuthenticationDetails,将想要的更多信息存储到details中。

首先创建MyWebAuthenticationDetails继承WebAuthenticationDetails,WebAuthenticationDetails提供了带有HttpServletRequest 参数的的构造方法,正合我们意,在构造方法里记录我们想要的值。

public class MyWebAuthenticationDetails extends WebAuthenticationDetails {
    //定义参数
    private String getAuthType;
    private String getMethod;
    ....

    public MyWebAuthenticationDetails(HttpServletRequest req) {
        //TO DO
    }

}

紧接着,仿照流程中的顺序,实现AuthenticationDetailsSource接口返回MyWebAuthenticationDetails 。

@Component
public class MyWebAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest,MyWebAuthenticationDetails> {
    @Override
    public MyWebAuthenticationDetails buildDetails(HttpServletRequest context) {
        return new MyWebAuthenticationDetails(context);
    }
}

最后在配置类替换下Spring Security系统默认提供的WebAuthenticationDetailsSource。

@Autowired
MyWebAuthenticationDetailsSource myWebAuthenticationDetailsSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .authenticationDetailsSource(myWebAuthenticationDetailsSource)
        .and()
        ...
}

使用方法和之前的一样,只是强转下类型就行。

 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
 MyWebAuthenticationDetails details = (MyWebAuthenticationDetails) authentication.getDetails();
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-10-30 12:49:11  更:2021-10-30 12:49:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年7日历 -2024/7/1 21:46:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码