스프링 시큐리티 설정
1. web.xml에서 아래와 같이 설정한다.
context-param 태그로 시큐리티 환경설정 파일의 이름을 지정해준다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
:
:
/WEB-INF/security-context.xml
</param-value>
</context-param>
filter 태그를 이용하여 시큐리티 설정을 한다.
<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>
2. 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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<context:component-scan base-package="com.sp"/>
<beans:bean id="authSuccess" class="com.sp.member.MyAuthSuccess"/>
<http auto-config="true" use-expressions="true" access-denied-page="/noAuth.action">
<!--access-denied-page="/noAuth.action" 접속권한이 없는 유저가 접속할 경우 이동할 주소 -->
<form-login login-page="/login/login.action" default-target-url="/" always-use-default-target="true" authentication-failure-url="/login/login.action?error" authentication-success-handler-ref="authSuccess"/>
<!-- always-use-default-target="true" : 로그인 성공후에 default-target-url에 설정한 곳으로 갈지 말지 설정 -->
<!-- authentication-failure-url="/login/login.action?error" : 로그인 실패하면 보내는 주소 -->
<!-- authentication-success-handler-ref : 로그인 성공후에 실행될 클래스 -->
<session-management>
<concurrency-control max-sessions="1" />
<!-- max-sessions="1" 세션을 한개만 허용(새로운 세션을 접속하게 하고 기존에 접속한 세션을 죽임),
<concurrency-control error-if-maximum-exceeded="true" /> 로그인을 하나의 브라우져만 가능케함
(크롬과 IE창을 같이 띄어놓고 먼저 크롬에서 접속한 상태에서 IE로 접속을 못함(새로운 세션 접속을 차단함, 단점은 로그아웃을 해도 세션이 안풀릴때가 있어서 로그인이 안될경우가 발생한다고 함, 되도록 이 설정은 하지말라네)-->
</session-management>
</http>
<!-- 로그인 할 때 사용자 정보와 권한을 확인하는 쿼리 -->
<jdbc-user-service data-source-ref="dataSource" id="userService"
users-by-username-query="SELECT userId AS userName, userPwd AS password,enabled FROM members WHERE userId=?"<!--enabled 컬럼 값이 '1'인 회원만 정상로그인-->
authorities-by-username-query="SELECT userId AS userName, authority FROM authorities WHERE userId=?"/>
<authentication-manager>
<authentication-provider user-service-ref="userService">
<password-encoder hash="sha-256"/><!-- hash="sha-256" -> 암호화 방식을 설정 -->
</authentication-provider>
</authentication-manager>
</beans:beans>
3. java에서 어노테이션을 이용하여 설정해준다.
1) controller.java에서 접속하기 위해 필요한 권한을 다음과 같이 설정한다.
:
@Secured("ROLE_ADMIN") // ROLE_ADMIN 권한을 가진 유저만 접속가능
@RequestMapping(value="/admin/main")
public String main(HttpServletRequest req) {
return "admin/main";
}
:
2) 회원가입 controller에서 다음과 같은 설정을 해준다.
ShaPasswordEncoder encoder=new ShaPasswordEncoder(256); // 암호화 방식인 sha-256 객체구함
String h=encoder.encodePassword(member.getUserPwd(), null); // 사용자의 실제 비밀번호를 sha-256로 암호화함.
member.setUserPwd(h); // 암호화된 비밀번호로 셋팅함
int result=memberService.insertMember(member);
3) 로그인 이후에 세션과 쿠키 처리를 할 수 있는 클래스를 만든다.
SavedRequestAwareAuthenticationSuccessHandler를 상속받은 뒤
onAuthenticationSuccess 메소드를 override 한다.
'spring' 카테고리의 다른 글
스프링 정적 페이지 처리 static data control (0) | 2016.07.17 |
---|---|
AbstractApplicationContext 스프링 설정파일 여러개 사용하기 (0) | 2014.05.02 |
트랜젝션 처리하기(연관된 일련의 작업이 실행도중 실패하면 그전 성공했던 작업을 취소하는 작업) (0) | 2013.08.10 |