Spring Security 의 권한 수여, 웹 보호 및 보호 방법 을 정리 합 니 다.
Spring Security 에 관 한 Authentication
* 권한 부여 에 대하 여
AcessDecision Manager 는 권한 을 수 여 받 았 습 니 다.구체 적 인 권한 수여 (authorization) 작업 은 일련의 Voter 에 게 맡 겨 진 것 이다.모든 Voter 는 AccessDecisionVoter 인터페이스의 vote 방법 을 실현 하고 int ACCESS 로 돌아 갑 니 다.GRANTED = 1;(찬성 표를 던 졌 다)
int ACCESS_ABSTAIN = 0;(기권 표를 던 지고)
int ACCESS_DENIED = -1; (반대 표를 던 졌 다)
AccessDecisionManager 는 세 가지 실현 이 있 습 니 다.
Affirmative Based - 최소한 한 명의 유권자 가 방문 허용 표를 던 졌 을 때 방문 을 허용 합 니 다.
Consensus Based - 모든 유권자 가 접근 허용 표를 던 질 때 접근 허용
Unanimous Based - 투표 자가 방문 거부 표를 던 지지 않 았 을 때 접근 할 수 있 습 니 다.
Spring Security 는 실 용적 인 voter 를 제공 합 니 다.
RoleVoter participates in a vote when the secured resource has a configuration attribute whose name starts with ROLE_.
* 웹 보호 에 대하 여
Spring Security 는 filter chain 보호 웹 응용 프로그램 을 제공 합 니 다.
FilterToBeanProxy 주의
다음 문 제 는 FilterToBean Proxy 의 역할: filter 는 웹. xml 에 설정 되 어 있 습 니 다. IOC 개념 이 있 을 수 없습니다. 서 비 스 는 filter 에 자동 으로 주사 할 수 없습니다. 이때 하나의 방법 은 WebApplication ContextUtils (예 를 들 어 WebApplication ContextUtils. getWebApplication Context (servlet Context). getBean ("security Manager") 을 사용 하 는 것 입 니 다.SpringSide 에 따 르 면 웹 애플 리 케 이 션 ContextUtils 는 주방, 화장실, 잔디, 지붕 등 비상 식적 인 장소, 땀 에 적합 하 다.(로드 존 슨 의 침입 론 은 감기 에 걸 리 지 않 았 습 니 다. 이 건 순 전 히 EJB 의 벽돌 을 때 리 는 것 같 습 니 다. 깊이 연구 할 가치 가 없습니다. pojo 팬 들 은 제 벽돌 을 찍 을 것 같 습 니 다. 저 는 고슴도치 갑 을 입 었 습 니 다. 침입 론 이 깊 어 진 후에 사람들 은 오히려 Spring 의 가끔 침입 에 대해 지적 하고 싶 어 합 니 다. 또한 Spring 을 좋아 하지 않 는 두 가지 측면 은 DDD 의 측면 에서 봄 이 자 연 스 럽 지 못 하고 contract fi 입 니 다.rst 저 는 헛소리 라 고 생각 합 니 다. 나중에 블 로그 에서 제 생각 을 함부로 뿌 렸 을 수도 있 습 니 다.) 침입 을 해결 하 는 방법 은 FilterToBean Proxy 입 니 다. 진정한 업 무 를 target class 에 대리 하고 spring 은 target class 라 는 이름 을 받 은 후에 관리 합 니 다. proxy 는 간단 한 모델 이지 만 여기 서 사용 하 는 것 은 교묘 합 니 다.
본론 으로 돌아 가면 filter chain 에는 적어도 네 개의 filter 가 포함 되 어 있 습 니 다.
Integration Filter - 이전의 authentication 을 가 져 오 면 보통 session 에서 가 져 옵 니 다.
AuthenticationProcessing Filter - 로그 인 할 때 와 같은 authentication request 처리
Exception Translation Filter - 전형 적 인 처 리 는 AuthenticationException 을 로그 인 페이지 로 돌리 고 AccessNenied Exception 을 403 오류 페이지 로 돌 리 는 것 입 니 다.
Filter Security Interceptor - 이것 은 진정 으로 권한 처 리 를 하 는 것 입 니 다. AuthenticaionManager 와 AccessDecisionManager 를 연결 합 니 다. 그래서 FilterSecurity Interceptor 가 중점 입 니 다.
설정 예제 먼저 보기:
1: <beans:bean id="resourceSecurityInterceptor" class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
2: <beans:property name="authenticationManager" ref="authenticationManager"/>
3: <beans:property name="accessDecisionManager" ref="accessDecisionManager"/>
4: <beans:property name="objectDefinitionSource" ref="secureResourceFilterInvocationDefinitionSource" />
5: <beans:property name="observeOncePerRequest" value="false" />
6: <custom-filter after="LAST" />
7: </beans:bean>
여기 object DefinitionSource 속성 은 보 호 받 는 자원 을 정의 합 니 다. 웹 을 보호 할 때 '자원' 은 url 을 대표 합 니 다. method 를 보호 할 때 자원 은 method 를 대표 합 니 다. 보호 method 에 대해 서 는 나중에 토론 합 니 다. [spring security 학습 총 결 을 취사선택 적 으로 참조 합 니 다]우선 시스템 이 제공 하 는 Object DefinitionSource 인 터 페 이 스 를 알 아 보 겠 습 니 다. object DefinitionSource 속성 은 바로 이 인터페이스의 실현 류 를 가리 키 는 것 입 니 다. 그 중 중요 한 방법 은 ConfigAttribute Definition getAttributes (Object object) 입 니 다.방법 사용 자 는 자원 보호 에 대응 하 는 권한 정 보 를 가 져 옵 니 다. 이 방법 은 ConfigAttributeDefinition 대상 을 되 돌려 줍 니 다. 이 대상 에는 실제 List 목록 이 하나 밖 에 없습니다. ConfigAttributeDefinition 류 의 구조 함 수 를 사용 하여 이 List 목록 을 만 들 수 있 습 니 다. 이 경우 보안 차단 기 는 getAttributes (Object object) 를 호출 합 니 다.방법 은 ConfigAttributeDefinition 대상 을 가 져 오고 이 대상 과 현재 사용자 가 가지 고 있 는 Authentication 대상 을 accessDecisionManager 에 전달 합 니 다. accessDecisionManager 는 이 를 voter 들 에 게 전달 합 니 다. 이 투표 자 들 은 ConfigAttributeDefinition 대상 에서 자원 보호 에 필요 한 권한 정 보 를 저장 하 는 목록 을 가 져 온 다음 이 목록 을 옮 겨 다 니 며 Aut 와 함께 합 니 다.hentication 대상 에서 Granted Authority [] 데이터 의 사용자 권한 정보 가 일치 합 니 다. 일치 하 는 데 성공 하면 투표 자 는 찬성 표를 던 집 니 다. 그렇지 않 으 면 반대 표를 던 집 니 다. 마지막 으로 access DecisionManager 는 이 투 표를 통 해 사용자 가 이 자원 에 접근 할 수 있 는 지 여 부 를 결정 합 니 다.만약 당신 이 role voter 를 사용한다 면, 돌아 오 는 ConfigAttribute Definition 은 사실 일련의 Role XXX 입 니 다.
여기 서 앞서 말 한 인증, 권한 수여 와""보호 웹" 은 세 가지 개념, user, role, resource 와 관련 되 어 있 습 니 다. 인증 과정 은 당신 의 신분 을 감정 하 는 동시에 role 도 관련 되 어 있 습 니 다. user 와 role 은 여러 쌍 입 니 다. 권한 수 여 는 role 과 resource 간 의 관 계 를 처리 하 는 것 이자 여러 쌍 입 니 다. 전형 적 인 resource 는 servlet URL 경로 입 니 다. 실전 적 인 측면 에서 볼 때 authentication 은 우리 가 해 야 할 두 가지 일이 있 습 니 다.: 하 나 는 UserService. loadUserByUsername (String userName) 을 통 해 인증 을 완성 하 는 본업 입 니 다. 다른 하 나 는 Users. getAuthorities () 를 실현 하여 user 와 role 을 연결 하 는 것 입 니 다.
* 보호 방법:
Spring 은 두 가지 방식 으로 보호 하 는 방법 을 제공 합 니 다. 하 나 는 AOP 이 고 다른 하 나 는 annotation 입 니 다.
AOP:
위의 object DefinitionSource 에 대한 소개 와 소결 부분 을 알 아 보 았 다 면 설정 을 직접 보면 쉽게 알 수 있 을 것 입 니 다.
1:
2: <bean id="autoProxyCreator"
3: class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
4:
5: <property name="interceptorNames">
6:
7: <list>
8:
9: <value>securityInterceptor</value>
10:
11: </list>
12: </property>
13: <property
14: name="beanNames">
15:
16: <list>
17:
18: <value>courseService</value>
19:
20: <value>billingService</value>
21:
22: </list>
23: </property>
24: </bean>
25:
26: <bean id="securityInterceptor"
27:
28: class="org.acegisecurity.intercept.method.MethodSecurityInterceptor">
29:
30: <property name="authenticationManager">
31: <ref
32: bean="authenticationManager"/>
33: </property>
34:
35: <property name="accessDecisionManager">
36: <ref
37: bean="accessDecisionManager"/>
38: </property>
39:
40: <property name="objectDefinitionSource">
41:
42: <value>
43:
44: com.springinaction.springtraining.service.CourseService.createCourse=ROLE_ADMIN
45:
46: com.springinaction.springtraining.service.CourseService.enroll*=ROLE_ADMIN,ROLE_REGISTRAR
47:
48: </value>
49: </property>
50: </bean>
위의 역수 몇 줄 이 방법 과 대응 하 는 role 을 정 의 했 는 지 주의 하 세 요.
annotation:
최종 목 표 는 이러한 방식 으로 권한 을 정의 하 는 것 이다.
1: /**
2: * @@org.acegisecurity.SecurityConfig("ROLE_ADMIN")
3: * @@org.acegisecurity.SecurityConfig("ROLE_REGISTRAR")
4: */
5: public void enrollStudentInCourse(Course course, Student student) throws CourseException;
멋 있어 보이 지만 하나의 drawback 은 제품 을 만 들 고 role 과 method (capability) 기능 을 유연 하 게 설정 할 수 있다 면 annotation 은 적용 되 지 않 습 니 다. annotation 은 code 에 쓰 여 있 기 때문에 copile time 은 role 과 method 사이 의 map 를 죽 였 습 니 다.
이것 은 이론 적 논리 가 없어 서 spring in action 의 설정 을 직접 붙 입 니 다.
1:
2: <bean
3: id="attributes"class="org.springframework.metadata.commons.CommonsAttributes"/>
4:
5: <bean id="objectDefinitionSource"
6: class="org.acegisecurity.intercept.method.MethodDefinitionAttributes">
7:
8: <property name="attributes"><ref
9: bean="attributes"/></property>
10: </bean>
11:
12: <bean id="securityInterceptor"
13:
14: class="org.acegisecurity.intercept.method.MethodSecurityInterceptor">
15: …
16:
17: <property name="objectDefinitionSource">
18: <ref
19: bean="objectDefinitionSource"/>
20:
21: </property>
22: </bean>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.