2017-05-04 131 views
0

我有一個使用Jersey的JAX-RS項目。現在我正在嘗試添加Spring Security的方法級安全性,但不幸的是它不工作,儘管intercept-url xml方式工作正常。@Secured在Jersey項目中整合Spring Security時不起作用

  1. 加在我的pom.xml
  2. 依賴更新web.xml

    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
        /WEB-INF/security.xml, 
        /WEB-INF/beans.xml 
    </param-value> 
    </context-param> 
    <!-- this is default security impl name used by deletetingFiterProxy --> 
    <filter> 
        <filter-name>springSecurityFilterChain</filter-name> 
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
        <filter-name>springSecurityFilterChain</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    
  3. 更新/WEB-INF/security.xml

    <beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security.xsd"> 
        <!-- kind of authentication applied 1) Basic 2) form-based etc.. auto-config="true" use-expressions="true"--> 
        <http auto-config="true"> 
         <http-basic /> 
        </http> 
    
        <!-- this allow to enable security annotations in restful resoruces --> 
        <global-method-security secured-annotations="enabled" /> 
    
        <!-- for defining users and roles --> 
        <authentication-manager> 
         <authentication-provider> 
          <user-service> 
           <user name="admin" password="admin" authorities="ROLE_CUSTOMER,ROLE_ADMIN"/> 
           <user name="student" password="student" authorities="ROLE_CUSTOMER"/> 
          </user-service> 
         </authentication-provider> 
        </authentication-manager> 
    </beans:beans> 
    
  4. 詮釋服務inteface方法

    public interface StudentServiceInterface { 
    
        @GET 
        @Path("/students") 
        @Secured("ROLE_CUSTOMER") 
        public Response getStudents(); 
    
        @GET 
        @Path("/students/{id}") 
        @Secured("ROLE_CUSTOMER") 
        public Response getStudent(@PathParam("id") int id); 
    
        @POST 
        @Path("/students") 
        @Consumes(MediaType.APPLICATION_JSON) 
        @Secured("ROLE_ADMIN") 
        public Response addStudent(Student stu); 
    } 
    

現在,當我嘗試訪問它打開資源的學生(/student)類不問密碼。

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

StudentServiceInterface接口實現

@Path("/db") 
@Produces(MediaType.APPLICATION_JSON) 
public class StudentService implements StudentServiceInterface{ 

    static StudentDao data= new StudentDaoImpl(); 

    @Override 
    public Response getStudents(){ 
     GenericEntity<List<Student>> entity = new GenericEntity<List<Student>>(data.getAllStudents()){}; 
     return Response.ok(entity).build(); 
    } 

    @Override 
    public Response getStudent(@PathParam("id") int id){ 
     return Response.ok(data.getStudent(id)).build(); 
    } 


    @Override 
    public Response addStudent(Student stu) { 
     data.addStudent(stu); 
     return Response.ok(stu).build(); 
    } 

} 
+1

Jersey verion 2.25.1 –

回答

1

你必須使用對Spring DI進一步擴展,見Jersey 2.25.1 User Guide

新澤西州提供了一個擴展,支持Spring DI。這使Jersey能夠使用Spring bean作爲JAX-RS組件(例如資源和提供者),並且允許Spring注入Jersey管理的組件。

Spring擴展模塊配置基於註釋。 Spring bean被注入並且JAX-RS類被Spring使用註解管理。注入的Spring bean可以使用Spring XML配置注入進一步的依賴關係。 Spring支持單例和請求範圍。爲了使JAX-RS資源能夠工作Spring需要代理的功能,例如Spring事務管理(帶有@Transactional),Spring Security和麪向方面的編程(例如@Aspect),資源必須由Spring來管理通過用@Component,@Service,@Controller或@Repository註釋:

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import org.springframework.stereotype.Component; 

@Component 
@Path("/") 
public class SomeResource { 

    @Transactional 
    @GET 
    public void updateResource() { 
     // ... 
    } 
} 

限制:

彈簧豆不能直接通過使用Spring XML配置

注入JAX-RS類

25.1。依賴

如果你想使用澤西春DI支持,您將需要球衣,spring3模塊添加到您的依賴性列表:

<dependency> 
    <groupId>org.glassfish.jersey.ext</groupId> 
    <artifactId>jersey-spring3</artifactId> 
    <version>2.25.1</version> 
</dependency> 

上述模塊增加了對Spring模塊傳遞依賴。有關依賴關係的列表和範圍的更多詳細信息,請參閱jersey-spring3模塊依賴關係。請注意,該模塊依賴於用於將Spring服務注入HK2服務或將HK2服​​務注入Spring服務的Spring/HK2 Bridge。

相關問題