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

标签 java jquery ajax spring spring-boot

我正在尝试使用 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/

相关文章:

java - 在新窗口中添加文本

php - JQuery 下载按钮

javascript - 加载未显示在框内

php - 使用ajax登录表单,无需刷新即可提交

jquery - 电影数据库: Return Movie Overview JSON

Java数据结构

java - 自定义maven插件: saving pom NOT effective pom

java - 如何使用不带注释的Spring Data JDBC?

php - Ajax 使用方法 load() 获取选择字段中的选项值

javascript - ajax 中的 Cross Origin 不适用于 IOS (10.3.1) 中的 .properties 文件