Java spring boot中有没有没有没有spring security的安全模块
我使用的是spring boot版本(2.0.1),我的安全性有问题,所以当我尝试使用ajax发出请求时,如下所示:Java spring boot中有没有没有没有spring security的安全模块,java,spring,spring-boot,Java,Spring,Spring Boot,我使用的是spring boot版本(2.0.1),我的安全性有问题,所以当我尝试使用ajax发出请求时,如下所示: $.ajax({ url : "http://localhost:8080/utilisateurs/addUser", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
$.ajax({
url : "http://localhost:8080/utilisateurs/addUser",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
type : "POST",
data : user,
dataType : "application/json"
}
package com.sid.webService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
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;
@CrossOrigin
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public boolean addUser(@RequestBody Utilisateur u)
{
try
{
mr.ajouterUtilisateur(u);
return true;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
}
@CrossOrigin(origins="http://localhost:8080/utilisateurs/")
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public boolean addUser(@RequestBody Utilisateur u)
{
try
{
mr.ajouterUtilisateur(u);
return true;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
}
我收到一个HTTP错误403,我知道这个错误的含义(用户可以登录服务器,但没有正确的权限),但问题是我没有使用任何安全模块这是我的dependency pom.xml文件:
<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>
org.springframework.boot
提前感谢。答案是通过向web服务添加@CrossOrigin注释来管理服务器端的COR,web服务如下所示:
$.ajax({
url : "http://localhost:8080/utilisateurs/addUser",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
type : "POST",
data : user,
dataType : "application/json"
}
package com.sid.webService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
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;
@CrossOrigin
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public boolean addUser(@RequestBody Utilisateur u)
{
try
{
mr.ajouterUtilisateur(u);
return true;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
}
@CrossOrigin(origins="http://localhost:8080/utilisateurs/")
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public boolean addUser(@RequestBody Utilisateur u)
{
try
{
mr.ajouterUtilisateur(u);
return true;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
}
如果您想要更多的定制,请添加您将从中访问spring后端的所有域,并在属性源中创建它们,如下所示:
$.ajax({
url : "http://localhost:8080/utilisateurs/addUser",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
type : "POST",
data : user,
dataType : "application/json"
}
package com.sid.webService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
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;
@CrossOrigin
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public boolean addUser(@RequestBody Utilisateur u)
{
try
{
mr.ajouterUtilisateur(u);
return true;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
}
@CrossOrigin(origins="http://localhost:8080/utilisateurs/")
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public boolean addUser(@RequestBody Utilisateur u)
{
try
{
mr.ajouterUtilisateur(u);
return true;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
}
那个脚本在哪里?它是否来自您的应用程序?谢谢@MarkoPacak,是的,如果您看到我之前的帖子,您将看到web服务的代码。在我看来,您似乎被CORS筛选器阻止,这意味着脚本驻留在您的应用程序之外。但也许我错了…@MarkoPacak在这种情况下我能做什么?你应该首先了解是什么,以及为什么会触发选项飞行前请求以及为什么会发生所有这些。正如我所说,我的最佳猜测是您的web资源位于web应用程序之外,因此您的服务器不信任它(谢天谢地)。您不应该删除CORS过滤器,而应该集成应用程序提供的资源(js、css等等)。与另一个问题中有人指出的相反,CSRF与此无关