AJAX/Spring MVC-没有Spring安全性的403禁止的错误 [英] AJAX / Spring MVC - 403 Forbidden Error without Spring Security
问题描述
我正在尝试使用Spring MVC从Web服务发出Ajax请求:
I'm trying to make ajax request from my web service with Spring MVC :
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安全,所以如何阻止该权限,这是我对spring boot的配置:
the problem here is the user is login but don't have the right permission , the real issu is that i'm not using spring security , so how block the permission , this is my configuration of 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
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
错误消息:
- jquery.min.js:2选项 http://localhost:8080/utilisateurs/addUser 403.
- 无法加载 http://localhost:8080/utilisateurs/addUser :飞行前响应已无效的HTTP状态代码403.
- jquery.min.js:2 OPTIONS http://localhost:8080/utilisateurs/addUser 403.
- Failed to load http://localhost:8080/utilisateurs/addUser: Response for preflight has invalid HTTP status code 403.
这是我的pom.xml:
this is my 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>
有人可以告诉我这是怎么回事,我是在犯一些错误吗? 预先谢谢你.
can someone tell me what is wrong here, am I doing some mistakes?? thank you in advance.
推荐答案
请参阅Maven依赖项,了解是否已将spring-security
包含到项目中.如果您没有将依赖项包含在依赖项中,则spring boot将自动配置spring安全性,而无需任何依赖.删除它.删除后它将起作用.依赖项位于pom.xml
Do see the Maven Dependencies on whether you have included spring-security
to your project spring boot will automatically configure spring security without single if you have included in the dependencies .Remove it .It will work after removing .Dependencies will be in pom.xml
这篇关于AJAX/Spring MVC-没有Spring安全性的403禁止的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!