2017-05-04 14 views
0

Jerseyを使用したデモJAX-RSプロジェクトがあります。今私は春のセキュリティのメソッドレベルのセキュリティを追加しようとしていますが、残念ながらそれは動作しませんintercept-url xmlの方法はうまくいきます。 web.xml/WEB-INF/security.xmlJerseyプロジェクトでSpring Securityを統合する際に@Securedが動作しない

<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> 
  • 更新

    <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> 
    
  • として注釈サービスintefaceを法

    を更新する私のpom.xml
    1. 追加されたすべての依存関係今
      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インタフェースの実装あなたは春のDIのために拡張子を使用する必要が

    @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

    ジャージーバージョン2.25.1 –

    答えて

    1

    Jersey 2.25.1 User Guideを参照してください。

    ジャージーは、春のDIをサポートするための拡張機能を提供します。これにより、JerseyはJAX-RSコンポーネント(リソースやプロバイダなど)としてSpring Beanを使用できるようになり、SpringがJersey管理対象コンポーネントに注入することも可能になります。

    Spring拡張モジュールの設定は注釈に基づいています。 Spring Beanがインジェクトされ、JAX-RSクラスはアノテーションを使用してSpring管理されます。注入されたSpring Beanは、Spring XML設定を使用してさらに依存関係を注入できます。スプリングシングルトンとリクエストスコープがサポートされています。

    Springトランザクション管理(@Transactional)、Spring Security、アスペクト指向プログラミング(@Aspectなど)などのプロキシ処理が必要なSpring機能をJAX-RSリソースで動作させるには、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 Beanは、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> 
    

    上記モジュールは春モジュールの推移依存関係を追加します。依存関係のリストとスコープの詳細については、jersey-spring3モジュールの依存関係を参照してください。このモジュールは、SpringサービスをHK2サービスにインジェクトしたり、HK2サービスをSpringサービスに注入するために使用されるSpring/HK2ブリッジに依存しています。

    関連する問題