저지를 사용하는 데모 JAX-RS 프로젝트가 있습니다. 이제 스프링 보안의 메소드 레벨 보안을 추가하려고 시도하지만 불행히도 intercept-url
xml 방식이 정상적으로 작동하지만 작동하지 않습니다. web.xml
/WEB-INF/security.xml
Jersey 프로젝트에서 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
에서
- 추가 된 모든 종속성
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();
}
}
저지 버전 2.25.1 –