首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring boot中有没有没有spring security的安全模块

spring boot中有没有没有spring security的安全模块
EN

Stack Overflow用户
提问于 2018-05-22 22:24:30
回答 1查看 37关注 0票数 0

我使用的是spring boot版本(2.0.1),并且我在安全性方面有问题,所以当我尝试使用ajax发出请求时,如下所示:

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

我收到一个HTTP error 403,我知道这个错误的含义(用户可以登录服务器,但没有正确的权限),但问题是我没有使用任何安全模块这是我的依赖pom.xml文件:

代码语言:javascript
复制
<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>

那么谁可以拦截这个请求,spring boot中有没有内部模块可以实现安全性,请看my previous post

提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2018-05-24 05:25:02

答案是通过向web服务添加@CrossOrigin注释来管理服务器端的CORS,web服务如下所示:

代码语言:javascript
复制
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;
    }   
  } 
}

如果你想要更多的定制,添加你将要访问你的spring后端的所有域,并像这样在属性源中创建它们:

代码语言:javascript
复制
@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;
    }   
  } 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50470134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档