본문 바로가기

Web

Spring Security

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.





https://projects.spring.io/spring-security/



Dependency

▶Maven Project를 생성하고 Pom.xml에 아래 내용 추가


▶최신 버전은 Spring Security Project 사이트 참고



        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>







Web.xml
빨간색 부분 추가


context-security.xml에 세부 내용 작성 - 위치는 당연히 main/webapp/WEB-INF/spring/context-security.xml


▶모든 URL 접근filter



     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/context-mysql.xml,
            /WEB-INF/spring/context-security.xml
        </param-value>
    </context-param>

     <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>







context-security.xml


▶css, js, 그림 파일 등의 리소스 대해서 접근 허용 해 줘야 함

                <http pattern="/resources/**" security="none">


로그인 관련 페이지 모든 사용자가 접속 가능하도록

                <http pattern="/LoginPage.do" security="none">


▶URL 패턴이 /permit/** 인 주소는 로그인하여 ADMIN으로만 접속 가능

   <intercept-url pattern="/permit/**" access="hasRole('ROLE_ADMIN')" />


http pattern 이 intercept-url pattern 보다 적용 레벨 높다. 이는 intercept-url pattern으로 권한을 주더라도 http pattern으로 설정한 값이 높게 적용됨


 login-page

 로그인 페이지

 login-processing-url

 로그인 form action 주소

 default-target-url

 로그인 성공 시 이동

 authentication-failure-url

 로그인 성공 시 이동 -> 예제는 다시 로그인 페이지로 이동

 username-parameter

 아이디

 password-parameter

 패스워드

 max-sessions 중복 로그인 허용
 expired-url

 세션 끊어질 경우 이동

 logout-url

 로그아웃 주소

 logout-success-url 로그아웃 성공 시 이동
 delete-cookies 세션이 무효화될 때 삭제될 쿠키이름
 invalidate-session 로그아웃 시 세션이 무효화
  



<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-4.0.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

    

   

    <http pattern="/resources/**" security="none"></http>
    <http pattern="/LoginPage.do" security="none"></http>
    <http pattern="/" security="none"></http>
    <http pattern="/permit/duplicate_user.do" security="none"></http>

    <http auto-config="true" use-expressions="true">
    <csrf disabled="true" />
    <intercept-url pattern="/logout.xml" access="permitAll()" />
    <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
    <intercept-url pattern="/permit/**" access="hasRole('ROLE_ADMIN')" />
    <access-denied-handler error-page="/403" />

    <form-login login-page='/LoginPage.do'
            login-processing-url="/login_security.xml (아래 JSP form)" default-target-url="/service1.do"
            always-use-default-target="true" authentication-failure-url="/LoginPage.do?error=true"
            username-parameter="id (아래 JSP form)" password-parameter="pw (아래 JSP form)" />

    <session-management>
            <concurrency-control max-sessions="1" expired-url="/LoginPage.do" />
    </session-management>

    <logout logout-url="/logout.xml (아래 JSP form)" logout-success-url="/LoginPage.do"
            delete-cookies="JSESSIONID" invalidate-session="true" />
    </http>






    <authentication-manager>
        <!-- Master ID --> 가입 절차없이 아래 아이디로 접속가능 (테스트용)
        <authentication-provider>
            <user-service>
                <user name="root" password="root" authorities="ROLE_ADMIN" />
            </user-service>
        </authentication-provider>

        <!-- authentication from database --> 데이터 베이스와 연결하여 인증
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="select id, pw_word, enabled from 테이블명 where id=?"
                authorities-by-username-query="select id, auth from odin.T_USER where id=?" />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

 







context-mysql.XML


     ....


    (주요 내용)

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


    ....








JSP

 


      [로그인]

      <form id="login_form" action="${pageContext.request.contextPath}/login_security.xml" method='POST' >           
            <div>
                <div class="input-icon">
                    <i class="fa fa-user"></i> <input id="id"
                        class="form-control placeholder-no-fix" type="text"
                        autocomplete="off" placeholder="Username" name="id" />
                </div>
            </div><br>
            <div>
                <label class="control-label visible-ie8 visible-ie9">Password</label>
                <div class="input-icon">
                    <i class="fa fa-lock"></i> <input id="pw"
                        class="form-control placeholder-no-fix" type="password"
                        autocomplete="off" placeholder="Password" name="pw" />
                </div>
            </div>
            <div>               
                <button id="login_BTN" class="btn green pull-left">Login</button>
            </div>
        </form>



        ...

        ...



        [로그 아웃 클릭]

        <div class="page-top">
                <div class="top-menu" style="margin-right : 40px;">
                    <p>
                           <a href="${pageContext.request.contextPath}/logout.xml">
                                  <img src="/odin/resources/login/img/btn_logout.png"/>
                           </a>
                    </p>
                </div>
         </div>













'Web' 카테고리의 다른 글

rabbitmq - web  (0) 2018.02.09
tomcat + apache 연동 (mok_jk)  (0) 2017.11.08
centos7 + tomcat8 자동실행  (0) 2017.11.08
엑셀(JXL) 다운로드  (0) 2017.04.27
Web Socket 사용법  (1) 2017.04.25