java - Spring Security 无法识别哪个登录用户
问题描述
我将 Spring Security 添加到我的 Spring Boot 应用程序,但之后,身份验证过程服务器在下一个请求中无法识别同一用户。我将 Angular 5 用于 UI,也许 UI 端的这个问题可能会被要求不包括 cookie。帮助我,请理解为什么 Spring Security 无法识别已登录的用户。
在网页配置中:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/rest/user/login").permitAll();
http.csrf().disable()
.authenticationProvider(authenticationProvider())
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.formLogin()
.permitAll()
.loginProcessingUrl("/rest/user/login")
.usernameParameter("username")
.passwordParameter("password")
.successHandler(logInSuccessHandler)
.failureHandler(authFailureHandler)
.and()
.logout().permitAll()
.logoutRequestMatcher(new AntPathRequestMatcher("/rest/user/logout", "POST"))
.logoutSuccessHandler(logoutHandler)
.and()
.sessionManagement()
.maximumSessions(1);
http.authorizeRequests().anyRequest().authenticated();
}
会话管理器已激活.sessionManagement().maximumSessions(1);
并且身份验证完成成功我的实现通过登录名和密码UserDetailsService
正确返回对象。User
但是对这个控制器的下一个请求:
@GetMapping("/rest/list")
public RestList list() {
...
}
重定向到:
@Component
public class UnauthorizedHandler implements AuthenticationEntryPoint {
@Override
public void commence(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException) throws IOException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}
}
我有登录检查器:
@Component
public class LoggedInChecker {
public User getLoggedInUser() {
User user = null;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object principal = authentication.getPrincipal();
// principal can be "anonymousUser" (String)
if (principal instanceof UserDetailsImpl) {
UserDetailsImpl userDetails = (UserDetailsImpl) principal;
user = userDetails.getUser();
}
}
return user;
}
}
我用这个检查器UserService
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private LoggedInChecker loggedInChecker;
@Override
public User getUserByUsername(String username) {
return userRepository.findByUsername(username);
}
@Override
public List<String> getPermissions(String username) {
User user = userRepository.findByUsername(username);
return nonNull(user) ? user.getRoles().stream().map(Role::getRole).collect(toList()) : Lists.newArrayList();
}
@Override
public User getCurrentUser() {
return loggedInChecker.getLoggedInUser();
}
@Override
public Boolean isCurrentUserLoggedIn() {
// This place must call but nothing happening
return nonNull(loggedInChecker.getLoggedInUser());
}
}
我以为 Spring 会自动调用我UserSevice
的检查授权。但是如何指定HttpSecurity
有关会话的保存信息?
在 Angular 方面,我使用HttpClient
:
@Injectable()
export class CoreApi {
private baseUrl = 'http://localhost:8080/rest/';
constructor(public http: HttpClient) {
}
public get(url: string = ''): Observable<any> {
return this.http.get(this.getUrl(url));
}
}
也许您知道如何指定 Spring Security 来检查身份验证LoggedInChecker
或其他方式,让我知道。谢谢你!
解决方案
推荐阅读
- apache-spark - 从 Kafka 批量写入不观察检查点并写入重复项
- php - 是否可以在 laravel 中为后端和前端创建单独的错误页面?
- javascript - React Native-Firebase:重新连接时如何关闭数据自动同步?
- c# - 使用相同逻辑的不同结果
- .net - 404 - 找不到文件或目录。您要查找的资源可能已被删除或重命名
- android - 为 Google Play 服务清除数据时的意图操作
- git - Git 恢复被 git fetch origin + git reset --hard 删除的本地文件
- python - AttributeError:模块 'socket' 没有属性 'MSG_DONTWAIT'
- java - 如何从 Java 独立应用程序在前台打开 Windows 应用程序
- c# - 为什么 SharePoint 客户端对象模型在查询失败时检索所有项目?