Spring3에서 Security 사용
다음과 같을때 유용하다.
1. 관리자에게 권할 별로 접근 메뉴가 다르다(일반운영자, 최종관리자)
2. 동시접속로그인을 제한한다.
url별로 접근 제한을 할 수 있고 권한에 대해 인증을 할 수 있고 로그아웃, 로그인, 세션생성, 비밀번호체크등 많은 클래스파일 작업이 필요없다.
참조 : http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity-single.html
1. web.xml 편집
1.1 contextConfigLocation 에 security.xml을 추가한다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
classpath:security-context.xml
</param-value>
</context-param>
* 여기서 주의할 점은 servlet에서 적용한 contextConfigLocation에 사용하지 말아야 한다.
이부분에 대해서는 http://actionscripter.tistory.com/28 를 참조 바란다.
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:web-context.xml
classpath:security-context.xml<!-- 이곳에 이렇게 넣지 마세요 -->
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
1.2 springSecurityFilterChain 의 filter와 filter-mapping 을추가
<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>
이로서 web.xml 수정되었다.
이렇게 적용한후 실행을 하게되면
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined 부분의 에러가 발생한다.
springSecurityFilterChain 을 정의해 주어야 한다는 내용인데 web.xml의 contextConfigLocation 에서 정의된 security-context.xml 파일을 수정하자
2. applicationContext-security.xml 편집
2.1 http 설정
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
그릭 다시 실행을 하면 No bean named 'org.springframework.security.authenticationManager' is defined 이 발생한다.
2.2
<authentication-manager>
<authentication-provider>
<user-service>
<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="bobspassword" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
을 함께 넣어주면 실제적으로 로그인 페이지가 뜨는 것을 확인할 수 있다.
이때 로그인을 하기위해서는 authentication-provider 에서 정의된 user name 과 password 를 넣어주면 된다.
[security-context.xml] ################################################################################################
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- ***************************************************************************** -->
<!-- This context file exists for developers to enter in their own security configurations. -->
<!-- ***************************************************************************** -->
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="bobspassword" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
댓글 없음:
댓글 쓰기