gpt4 book ai didi

java - AJAX/Spring MVC - 没有 Spring Security 的 403 禁止错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:47 25 4
gpt4 key购买 nike

我正在尝试使用 Spring MVC 从我的 Web 服务发出 ajax 请求:

package com.sid.webService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.sid.dao.entity.Utilisateur;
import com.sid.metier.IMetierUtilisateur;

@Component
@RestController
@RequestMapping("/utilisateurs")
public class webServiceUtilisateur {
@Autowired
private IMetierUtilisateur mr;

@RequestMapping(value="/addUser",method=RequestMethod.POST)
public boolean addUser(@RequestBody Utilisateur u)
{
try
{
mr.ajouterUtilisateur(u);
return true;
}
catch(Exception e)
{
return false;
}
}
}

使用此 AJAX 请求:

$.ajax({
url : "http://localhost:8080/utilisateurs/addUser",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
type : "POST",
data : user,
dataType : "application/json"
}

这里的问题是用户登录但没有正确的权限,真正的问题是我没有使用spring security,那么如何阻止权限,这是我的spring boot配置:

spring.datasource.url=jdbc:mysql://localhost:3306/ekka-e-commerce
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

错误消息:

这是我的 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.sid</groupId>
<artifactId>examplesJavaSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>ekka-e-commerce</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

有人可以告诉我这里出了什么问题吗?我做错了什么吗?预先感谢您。

最佳答案

请查看 Maven 依赖项,了解您的项目是否包含 spring-security 到项目中。如果您已包含在依赖项中,Spring Boot 将自动配置 spring security,而不需要单个。删除它。删除后它将起作用。依赖项将位于 pom.xml

关于java - AJAX/Spring MVC - 没有 Spring Security 的 403 禁止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50451207/

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