所以,我的朋友的登录页面出现了问题.当她try 向她的API发出请求时,它只会在前端出错,而在API中什么都不会发生.

这是出现在控制台上的错误: CORS策略已阻止从源"http://localhost:3000"访问位于"http://localhost:8082/login"的XMLHttpRequest:对预检请求的响应未通过访问控制判断:请求的资源上不存在"Check-Control-Allow-Origin"标头. Copyright © 2018 - 2019 www.js.com. All rights reserved.粤ICP备16048888号-1

这是登录端点

@Controller
@CrossOrigin(
    origins = "http://localhost:3000",
    allowCredentials = "true"
)
@RequestMapping("/login")
public class AuthenticationController {

@Autowired
AuthenticationManager authenticationManager;

private static final Logger logger = LoggerFactory.getLogger(AuthenticationController.class);

@PostMapping
public ResponseEntity<?> login(
        @RequestBody Login login,
        HttpServletRequest request,
        HttpServletResponse response
){
    logger.debug("Received login request for username: {}", login.getUsername());
    System.out.println("AuthenticationCOntroller");
    SecurityContextRepository securityContextRepository = new HttpSessionSecurityContextRepository();
    UsernamePasswordAuthenticationToken token =
            new UsernamePasswordAuthenticationToken(login.getUsername(), login.getPassword());
    Authentication authentication = authenticationManager.authenticate(token);

    if(authentication.isAuthenticated()){
        User user = (User) authentication.getPrincipal();
        Cookie cookie = CookieUtil.generateCookie(user);
        response.addCookie(cookie);
        return ResponseEntity.ok(authentication.getPrincipal());
    }
}

以下是她的GitHub链接: APIFront-end

她试图重构她的所有代码,但登录时仍然出错.

推荐答案

我可能有解决办法! 在您的身份验证管理器中,您可以使用两种方法来配置CORS. 你只能有一个:

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/login")
public class AuthenticationController {
@Autowired
AuthenticationManager authenticationManager;

@PostMapping
public ResponseEntity<?> login(
        @RequestBody Login login,
        HttpServletResponse response)
{

    SecurityContextRepository securityContextRepository = new 
HttpSessionSecurityContextRepository();
    UsernamePasswordAuthenticationToken token =
            new UsernamePasswordAuthenticationToken(login.getUsername(), 
login.getPassword());
    Authentication authentication = authenticationManager.authenticate(token);

    if(authentication.isAuthenticated()){
        User user = (User) authentication.getPrincipal();
        Cookie cookie = CookieUtil.generateCookie(user);
        response.addCookie(cookie);
        return ResponseEntity.ok(authentication.getPrincipal());
    }

    return ResponseEntity.status(401).build();

}
}

Java相关问答推荐

当切换javaFX场景时,stage的大小正在Minimize

最小拓Flutter 排序的时间复杂度是多少?

Junit with Mockito for java

调用引发泛型异常的泛型方法时出现编译错误

具有多种令牌类型和段的复杂Java 17正则表达式

Jakarta CDI强制bean构造/注册遗留事件侦听器

在Java中如何从Executors.newFixedThreadPool(MAX_THREAD_COUNT())迁移到虚拟线程

有没有更快的方法在N个容器中删除重复项?

对字符串长度进行排序,但颠倒了顺序(最长字符串在前)

如何在太阳系模拟器中添加月球?

如何在Java中为thunk创建映射器函数

Spring Framework6.1中引入的新RestClient是否有适合于测试的变体,就像RestTemplate和TestRestTemplate一样?

每次我需要时创建和关闭数据库连接会有什么效果吗?

如何在Record Java中使用isRecord()和RecordComponent[]?

Cucumber java-maven-示例表-未定义一步

如何使用外部函数从Java中获取C++ struct 的返回值&;内存API

java.lang.NoSuchMethodError:';org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.poi-poi-ooxml-5.2.4

带有提取器的JavaFXObservableList会根据侦听器的存在而改变行为

使用DynamoDB增强客户端时未更新属性

睡眠在 Spring Boot 中