Spring Security 中如何快速查看登录用户 IP 地址等信息?

接口的解释如下:

  1. getAuthorities 方法用来获取用户的权限。

  2. getCredentials 方法用来获取用户凭证,一般来说就是密码。

  3. getDetails 方法用来获取用户携带的详细信息,可能是当前请求之类的东西。

  4. getPrincipal 方法用来获取当前用户,可能是一个用户名,也可能是一个用户对象。

  5. isAuthenticated 当前用户是否认证成功。

这里有一个比较好玩的方法,叫做 getDetails。关于这个方法,源码的解释如下:

Stores additional details about the authentication request. These might be an IP address, certificate serial number etc.

从这段解释中,我们可以看出,该方法实际上就是用来存储有关身份认证的其他信息的,例如 IP 地址、证书信息等等。

实际上,在默认情况下,这里存储的就是用户登录的 IP 地址和 sessionId。我们从源码角度来看下。

2.源码分析


松哥的 SpringSecurity 系列已经写到第 12 篇了,看了前面的文章,相信大家已经明白用户登录必经的一个过滤器就是 UsernamePasswordAuthenticationFilter,在该类的 attemptAuthentication 方法中,对请求参数做提取,在 attemptAuthentication 方法中,会调用到一个方法,就是 setDetails。

我们一起来看下 setDetails 方法:

protected void setDetails(HttpServletRequest request,

UsernamePasswordAuthenticationToken authRequest) {

authRequest.setDetails(authenticationDetailsSource.buildDetails(request));

}

UsernamePasswordAuthenticationToken 是 Authentication 的具体实现,所以这里实际上就是在设置 details,至于 details 的值,则是通过 authenticationDetailsSource 来构建的,我们来看下:

public class WebAuthenticationDetailsSource implements

AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> {

public WebAuthenticationDetails buildDetails(HttpServletRequest context) {

return new WebAuthenticationDetails(context);

}

}

public class WebAuthenticationDetails implements Serializable {

private final String remoteAddress;

private final String sessionId;

public WebAuthenticationDetails(HttpServletRequest request) {

this.remoteAddress = request.getRemoteAddr();

HttpSession session = request.getSession(false);

this.sessionId = (session != null) ? session.getId() : null;

}

//省略其他方法

}

默认通过 WebAuthenticationDetailsSource 来构建 WebAuthenticationDetails,并将结果设置到 Authentication 的 details 属性中去。而 WebAuthenticationDetails 中定义的属性,大家看一下基本上就明白,这就是保存了用户登录地址和 sessionId。

那么看到这里,大家基本上就明白了,用户登录的 IP 地址实际上我们可以直接从 WebAuthenticationDetails 中获取到。

我举一个简单例子,例如我们登录成功后,可以通过如下方式随时随地拿到用户 IP:

@Service

public class HelloService {

public void hello() {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();

System.out.println(details);

}

}

这个获取过程之所以放在 service 来做,就是为了演示随时随地这个特性。然后我们在 controller 中调用该方法,当访问接口时,可以看到如下日志:

WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 127.0.0.1; SessionId: 303C7F254DF8B86667A2B20AA0667160

可以看到,用户的 IP 地址和 SessionId 都给出来了。这两个属性在 WebAuthenticationDetails 中都有对应的 get 方法,也可以单独获取属性值。

3.定制


当然,WebAuthenticationDetails 也可以自己定制,因为默认它只提供了 IP 和 sessionid 两个信息,如果我们想保存关于 Http 请求的更多信息,就可以通过自定义 WebAuthenticationDetails 来实现。

如果我们要定制 WebAuthenticationDetails,还要连同 WebAuthenticationDetailsSource 一起重新定义。

结合上篇文章的验证码登录,我跟大家演示一个自定义 WebAuthenticationDetails 的例子。

上篇文章我们是在 MyAuthenticationProvider 类中进行验证码判断的,回顾一下上篇文章的代码:

public class MyAuthenticationProvider extends DaoAuthenticationProvider {

@Override

protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

String code = req.getParameter(“code”);

String verify_code = (String) req.getSession().getAttribute(“verify_code”);

if (code == null || verify_code == null || !code.equals(verify_code)) {

throw new AuthenticationServiceException(“验证码错误”);

}

super.additionalAuthenticationChecks(userDetails, authentication);

}

}

不过这个验证操作,我们也可以放在自定义的 WebAuthenticationDetails 中来做,我们定义如下两个类:

public class MyWebAuthenticationDetails extends WebAuthenticationDetails {

private boolean isPassed;

public MyWebAuthenticationDetails(HttpServletRequest req) {

super(req);

String code = req.getParameter(“code”);

String verify_code = (String) req.getSession().getAttribute(“verify_code”);

if (code != null && verify_code != null && code.equals(verify_code)) {

isPassed = true;

}

}

public boolean isPassed() {

return isPassed;

}

}

@Component

public class MyWebAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest,MyWebAuthenticationDetails> {

@Override

public MyWebAuthenticationDetails buildDetails(HttpServletRequest context) {

return new MyWebAuthenticationDetails(context);

}

}

首先我们定义 MyWebAuthenticationDetails,由于它的构造方法中,刚好就提供了 HttpServletRequest 对象,所以我们可以直接利用该对象进行验证码判断,并将判断结果交给 isPassed 变量保存。如果我们想扩展属性,只需要在 MyWebAuthenticationDetails 中再去定义更多属性,然后从 HttpServletRequest 中提取出来设置给对应的属性即可,这样,在登录成功后就可以随时随地获取这些属性了。

最后在 MyWebAuthenticationDetailsSource 中构造 MyWebAuthenticationDetails 并返回。

定义完成后,接下来,我们就可以直接在 MyAuthenticationProvider 中进行调用了:

public class MyAuthenticationProvider extends DaoAuthenticationProvider {

@Override

protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

if (!((MyWebAuthenticationDetails) authentication.getDetails()).isPassed()) {

throw new AuthenticationServiceException(“验证码错误”);

}

super.additionalAuthenticationChecks(userDetails, authentication);

}

}

直接从 authentication 中获取到 details 并调用 isPassed 方法,有问题就抛出异常即可。

最后的问题就是如何用自定义的 MyWebAuthenticationDetailsSource 代替系统默认的 WebAuthenticationDetailsSource,很简单,我们只需要在 SecurityConfig 中稍作定义即可:

@Autowired

MyWebAuthenticationDetailsSource myWebAuthenticationDetailsSource;

我的面试宝典:一线互联网大厂Java核心面试题库

以下是我个人的一些做法,希望可以给各位提供一些帮助:

整理了很长一段时间,拿来复习面试刷题非常合适,其中包括了Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等,且还会持续的更新…可star一下!

image

283页的Java进阶核心pdf文档

Java部分:Java基础,集合,并发,多线程,JVM,设计模式

数据结构算法:Java算法,数据结构

开源框架部分:Spring,MyBatis,MVC,netty,tomcat

分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

image

还有源码相关的阅读学习

image

065365856)]

283页的Java进阶核心pdf文档

Java部分:Java基础,集合,并发,多线程,JVM,设计模式

数据结构算法:Java算法,数据结构

开源框架部分:Spring,MyBatis,MVC,netty,tomcat

分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

[外链图片转存中…(img-yd1WFL8v-1721065365857)]

还有源码相关的阅读学习

[外链图片转存中…(img-auYZtQHP-1721065365857)]

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值