느림보 개발

2. 접근 제한 설정 및 여러 권한을 가지는 사용자 설정 본문

코드 정리/Spring Security

2. 접근 제한 설정 및 여러 권한을 가지는 사용자 설정

@르르 2023. 1. 16. 21:17

 

security-context 

 

SampleController와 view 페이지 매핑 후 작업

특정 URI에 접근할 때 인터셉터를 이용해서 접근하는 제한하는 설정은 <security:intercept-url> 를 사용한다. 

사용시엔 pattern 속성과 access라는 속성을 지정해야한다. 

pattern : URI 패턴 = 경로 

access : 권한 체크 

 

<security:http>

	<security:intercept-url pattern="/sampe/all" access="permitAll" />
	
	<security:intercept-url pattern="/sample/member" access="hasRole('ROLE_MEMBER')"/>
    
    <security:intercept-url pattern="/sample/admin" access="hasRole('ROLE_ADMIN')"/>

	.. 생략 .. 
	
</security:http>

 

 

 

 

실행시

/sample/all 는 모두가 접근 가능하기에 설정한 뷰페이지 출력

/sample/member는 회원 권한을 가지고 있는 사람만 접근 가능하기에 로그인 페이지로 이동

 

 

 

 

 

UserDetailsService

 

- 인증과 권한에 대한 실제 처리 담당 

- security-context.xml에서 처리 

- 테스트용으로 member/member -ROLE_MEMBER 권한을 가지는 사용자 추가 

<security:authentication-manager>
	
	<security:authentication-provider>
		<security:user-service>
		
			<!-- <security:user name="member" password="{noop}member" authorities="ROLE_MEMBER" /> -->
            <security:user name="member" password="{noop}member" authorities="ROLE_MEMBER" />
		
		</security:user-service>
	
	</security:authentication-provider>
	
</security:authentication-manager>

 

 

실행시

'PasswordEncoder' 라는 존재가 없기 때문에 오류 발생 

- 시큐리티 5버전부터는 반드시 PasswordEncoder라는 존재를 이용하도록 변경되었다. 

- 임시방편으로 {noop}을 통해 입력받은 값 그대로 값 비교

 

 

재실행시 로그인하면 정상적으로 출력!

 

 

 

 

 


쿠키 삭제

 

로그아웃을 구현하지 않았기 때문에 개발자 도구 F12를 눌러 Application-Cookies-JSESSIONID 를 눌러 쿠키 삭제를 한다. 그리고 페이지 재호출 해주기 

 

 

 

 

 

 

 

 

 


security-context 

 

사용자가 ADMIN일 경우 일반 회원페이지와 관리자 페이지에 접근할 수 있게끔 권한 2개를 부여

 

<security:user name="admin" password="{noop}admin" authorities="ROLE_MEMBER, ROLE_ADMIN" />

 

 

실행시

admin으로 로그인 후 관리자, 회원 접근 페이지로 이동할 수 있음을 확인할 수 있다. 

 

 

 

 

 

 

'코드 정리 > Spring Security' 카테고리의 다른 글

6. 로그아웃  (0) 2023.01.21
5. 로그인 후 출력할 페이지 설정  (0) 2023.01.20
4. 커스텀 로그인 페이지  (0) 2023.01.18
3. 접근 제한 메시지의 처리  (0) 2023.01.17
1. 스프링 시큐리티 기본 설정  (0) 2023.01.14
Comments