상세 컨텐츠

본문 제목

Spring3.1 Security (DB적용)

Spring

by husks 2014. 12. 6. 17:56

본문

반응형

maven pom.xml에 dependency를 추가 시켜 줍니다.

 

 

pom.xml

<!-- Spring-security -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>

 

filter적용

 

아래 설정을 참고하시어 해당 내용을 추가 해주세요.

 

<!-- filter : security -->
<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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
            /WEB-INF/spring/security-context.xml
        </param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <listener>
        <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher
        </listener-class>
    </listener>
    
    <!-- filter : security -->
    <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>
    
    <!-- filter : sitemesh -->
    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- // filter : sitemesh -->
    
    <!-- filter : encoding -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- // filter : encoding -->

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
        
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
 
/WEB-INF/spring/appServlet/security-context.xml

security-context.xml 를 생성하고 내용을 채워줍니다.

 

security-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:security="http://www.springframework.org/schema/security"
         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-3.1.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

        
    <security:http pattern="/common/css/**" security="none" />
    <security:http pattern="/common/img/**" security="none" />
    <security:http pattern="/common/js/**" security="none" />
         
    <security:http auto-config="true"><!-- 적절하게 페이지 접근을 설정 -->
        <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/admin/*" access="ROLE_ADMIN" />
        <security:intercept-url pattern="/dashboard" access="ROLE_USER" />

        <security:form-login     login-page="/user/login"
                                default-target-url="/dashboard"
                                authentication-failure-url="/user/login?fail=true" />
        <security:logout logout-success-url="/dashboard" />
        <security:anonymous/>
    </security:http>
        
     <security:authentication-manager>
        <security:authentication-provider>
            <security:password-encoder hash="sha-256" />
            <security:jdbc-user-service 
                data-source-ref="dataSource"
                users-by-username-query="select id as username, password as password, 1 as enabled 
                    from USER where id = ?"
                authorities-by-username-query="select id as username, authority as authority 
                    from AUTHORITIES where id = ?" />
        </security:authentication-provider>
    </security:authentication-manager>

</beans>
 

 

시큐리티의 추가 개념은 여기를 참고 하면 좋을듯.

 

http://netframework.tistory.com/356

반응형

관련글 더보기

댓글 영역