【发布时间】:2016-11-13 05:33:26
【问题描述】:
我与用户有一张桌子。此表中有“ip_address”列。
我有服务器 - java 服务器 我在 Spring Boot 上有网络应用程序。
Spring boot 通过 rest 与 java server 通信。
我需要通过ip实现系统中的用户身份验证。
Whem 用户打开网页 - Spring Boot 应用程序获取 remoteIpAddress(String ipAddress = request.getRemoteAddr();) 并将其传递给 url 中的 java 服务器。 java 服务器在用户表中的 db 中检查此 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;
}
}
在此类中将用户保存到会话中。之后,当我打开网页时,我不需要输入登录名和密码。它包含在会话中。但是我的网页没有打开错误
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
【问题讨论】:
-
无配置,无堆栈跟踪。你真的调试过你的代码吗?另外恕我直言,注入
HttpServletRequest来获取您需要的信息是一个糟糕的主意。它是在Authentication上设置的WebAuthenticatioNDetails的一部分,请改用它。也不确定基于 IP 地址的身份验证是否是一个好主意,大公司呢,由于每个人都使用代理,他们通常只有一个外部 IP 地址。
标签: java spring spring-security