gpt4 book ai didi

java - Spring Security 跨源

转载 作者:行者123 更新时间:2023-11-30 01:43:11 24 4
gpt4 key购买 nike

我开发了一个 REST API。一切都运行良好,除了安全性(登录、获取登录用户等......)当我登录用户并尝试通过 /getWhoAmI 端点获取它时,我无法获取它(它返回 HttpStatus.NOT_FOUND 因为没有身份验证)。在 Postman 中,它工作得很好!

我执行以下场景:登录用户,然后通过/getWhoAmI 获取它。在 postman 中,它可以工作并返回登录的用户,但在我的 javascript 应用程序中,它返回 HttpStatus.NOT_FOUND。

另外,我尝试调试它,在 getWhoAmI 中,身份验证为 null (仅当我在 JavaScript 中调用它时,在 postman 中包含经过身份验证的用户)

这是/getWhoAmI端点

@GetMapping(path = "/getWhoAmI")
public ResponseEntity<UserModel> getWhoAmI(Authentication authentication) {

// if there is no authenticated user
if(authentication == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
UserModel loggedUser = userRepo.findByUsername(loggedUsername);

return new ResponseEntity<>(loggedUser, HttpStatus.OK);
}

这是我的SecurityConfig:

@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

private ApplicationUserDetailsService userDetailsService;

public SecurityConfig(ApplicationUserDetailsService userDetailService) {
this.userDetailsService = userDetailService;
}

protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll()
.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
.and().logout().logoutSuccessHandler((new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK))) // return HttpStatus.OK after successfull logout
.and().csrf().disable();
http.headers().frameOptions().disable();
}

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
auth.userDetailsService(userDetailsService);
}

}

登录端点:

@PostMapping(path = "/login")
public ResponseEntity<UserModel> login(
@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password) {

UserModel user = userRepo.findUserByUsernameAndPassword(username, password);

if(user != null) {
UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());

if(userDetails != null) {
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
System.out.println(authentication);
}

return new ResponseEntity<>(user, HttpStatus.OK);
}

return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

我的前端 JavaScript:

$.ajax({
method: "GET",
url: SERVER_URL + "/getWhoAmI",
complete: function(data) {
console.log(data.responseJSON);
}
});

最佳答案

根据交换的意见:

由于来自前端的调用是跨源请求 (CORS),因此 ajax 调用不会发送 session 标识符 cookie (JSESSIONID)。

XMLHttpRequest.withCredentials必须将标志设置为 true 才能使跨站点请求包含 Cookie 和授权 header 等凭据。

对于JQuery的ajax功能,可以通过xhrFields: { withCredentials: true }来完成。 axios 有类似的 withCredentials 标志。

关于java - Spring Security 跨源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59220371/

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