【问题标题】:intercept-url patterns for REST URLsREST URL 的拦截 url 模式
【发布时间】:2017-10-02 01:57:27
【问题描述】:

我正在设计一组用于访问 URL 的 REST API。根据我的要求,有两个网址:

http://localhost:3126/securitydemo/webapi/db/students 

查看所有学生无需访问和

http://localhost:3126/securitydemo/webapi/db/students/1 

只允许ROLE_USER

我的 Spring Security 配置:

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="**/students/*" access="hasRole('ROLE_USER')" />
    <http-basic/>
</http>

如果我使用**/students/* 模式,则不会出现基本的安全弹出窗口。如果我使用/**,它就可以正常工作。

如何拦截两个不同安全级别的网址?

我的 REST 服务类:

@Path("/db")
@Produces(MediaType.APPLICATION_JSON)
public class StudentService {
    static StudentDao data = new StudentDaoImpl();
    @Path("/students")
    @GET
    public Response getStudents(){
        GenericEntity<List<Student>> entity = new GenericEntity<List<Student>>(data.getAllStudents()){};
        return  Response.ok(entity).build();
    }

    @Path("/students/{id}")
    @GET
    public Response getStudent(@PathParam("id") int id){
        return  Response.ok(data.getStudent(id)).build();
    }
}

【问题讨论】:

标签: rest spring-security


【解决方案1】:

试试这个

<http auto-config="true" use-expressions="true">
        <intercept-url pattern="/securitydemo/webapi/db/students/1/**"   access="hasRole('ROLE_USER')" />
        <http-basic/>
    </http>

【讨论】:

  • /Students/1 这里 1 是一个路径变量,它不固定
  • 你不能使用 spring security try 方法级别注释 @PreAuthorize("permitAll()") 和 @PreAuthorize("hasRole('ROLE_USER')") 来做到这一点
猜你喜欢
  • 2011-07-11
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多