gpt4 book ai didi

java - 如何在 spring security 中仅通过 ip 地址对用户进行身份验证?

转载 作者:行者123 更新时间:2023-11-29 04:43:22 28 4
gpt4 key购买 nike

我有用户表。此表中有“ip_address”列。

我有服务器 - java 服务器我在 spring boot 上有网络应用程序。

Spring boot通过rest与java server通信。

我需要在系统中通过ip实现用户认证。当用户打开网页时 - spring boot 应用程序获取 remoteIpAddress(String ipAddress = request.getRemoteAddr();) 并将其传递给 url 中的 java 服务器。 java 服务器在表用户的数据库中检查此 ip,如果用户可以登录,则将此用户返回到 spring boot 应用程序。

我想通过 spring security 实现这一点。但是当我打开网页时,在浏览器中打开了输入登录名和密码的对话框窗口。但我不需要登录名和密码。如果用户不为空,我需要 - 授予对页面的访问权限并保存用户。

如果我输入登录名和密码并按“确定”按钮,我将转到我的 IPAddressBasedAuthenticationProvider

@Component
public class IPAddressBasedAuthenticationProvider implements AuthenticationProvider {
@Autowired
private HttpServletRequest request;
@Autowired
AuthService authService;

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String ipAddress = request.getRemoteAddr();
AuthLkUser authLkUserByIp = authService.getAuthLkUserByIp(ipAddress);

if (authLkUserByIp == null) return null;

boolean b = authService.checkAuthLkUser(authLkUserByIp);
if (b) return null;
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
grantedAuths.add(grantedAuthority);
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths);
result.setDetails(authentication.getDetails());
return result;
}

@Override
public boolean supports(Class<?> aClass) {
return true;
}
}

并且在这个类中将用户保存到 session 中。之后,当我打开网页时,我不需要输入登录名和密码。它包含在 session 中。但是我的网页没有打开错误

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jul 11 14:27:59 ALMT 2016
There was an unexpected error (type=Forbidden, status=403).
Access is denied

最佳答案

对于使用嵌入式 Apache Tomcat 容器运行的 Spring Boot 应用程序,您可以使用 Apache Tomcat 中的 org.apache.catalina.filters.RemoteAddrFilter。

 @Bean
public FilterRegistrationBean remoteAddressFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
RemoteAddrFilter filter = new RemoteAddrFilter();
// filter.setAllow("127.0.0.1");
filter.setAllow("127\\.0\\.0\\.1");
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.addUrlPatterns("/gs/serving-web-content/testParameters");
return filterRegistrationBean;
}

allow : A regular expression (using java.util.regex) that the remote client's IP address is compared to. If this attribute is specified, the remote address MUST match for this request to be accepted. If this attribute is not specified, all requests will be accepted UNLESS the remote address matches a deny pattern.

您可以引用这篇文章- How do I restrict access to my application by IP address?

关于java - 如何在 spring security 中仅通过 ip 地址对用户进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304078/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com