2017-05-04 69 views
-1

我正在设计一组用于访问URL的REST API。按我的要求有两个网址:REST URL的拦截URL模式

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

查看所有不需要的访问学生和

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

ROLE_USER是允许的。

我的春天安全配置:

<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(); 
    } 
} 
+2

的可能的复制(http://stackoverflow.com/questions/43704389/antmatchers [antMatchers该路径的任何开头匹配] -that-matches-any-beginning-of-path) – dur

+0

你缺少一个'/'。试试'/ **/students/*'。 – dur

回答

-2

试试这个

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

/Students/1 here 1是一个路径变量,它不是固定的 –

+0

你不能使用spring安全性try方法级别注释@PreAuthorize(“permitAll()”)和@PreAuthorize(“hasRole('ROLE_USER')”) –