Security
Java EE κΈ°λ°μ Enterprise Software Application μ μν ν¬κ΄μ μΈ λ³΄μ μλΉμ€μ΄λ€.
λ§μ½ μ€νλ§ μνλ¦¬ν° (Spring Security) λ₯Ό μ¬μ©νμ§ μλλ€λ©΄ μ체μ μΌλ‘ μΈμ 체ν¬λ₯Ό νκ³ redirect λ±μ ν΄μΌν κ²μ΄λ€.
Spring Security λ 보μμ ꡬμ±νλ λ€μ λκ°μ§ μμμ΄ μλ€.
- Principal (μ κ·Ό 주체)
- 보νΈλ λμμ μ κ·Όνλ μ¬μ©μ
- Authentication (μΈμ¦)
- μ¬μ©μκ° μ ν리μΌμ΄μ μμ μ μνν μ μλ Authorization (κΆν) μ μ»λ κ³Όμ
- Authorization (κΆν)
- μ ν리μΌμ΄μ μ μμ μ μνν μ μλλ‘ νλ½λμ΄ μλ κ²°μ
μΌλ°μ μΌλ‘ λ‘κ·ΈμΈ κ³Ό κ°μ κ³Όμ λ€μ HTTP κΈ°λ³ΈμΈμ¦μ ν΅νμ¬ μ§νλλ©° μ΄λ Form κΈ°λ° λ‘κ·ΈμΈ μ΄ λλ€.
μΈμ¦ (Authentication)
Spring Security μ μΈμ¦κ΅¬μ‘°λ μΈμ -μΏ ν€ λ°©μμΌλ‘ μ§νλλ€.
- μ¬μ©μκ° λ‘κ·ΈμΈμ μλ (Http Request)
- μΈμ¦ νν° (Authentication Filter) μμ User DB λ₯Ό νμΈν¨
- User DB μ μλ μ¬μ©μλΌλ©΄ ν΄λΉ μ¬μ©μ μ 보λ₯Ό μ‘°ννμ¬ μ¬μ©μμ μΈμ μ μμ±
- Spring Security μ μΈλ©λͺ¨λ¦¬ μΈμ
μ μ₯μμΈ
Security Context Holder
μ μ μ₯ - μ¬μ©μμκ²
Session ID
μ ν¨κ» μλ΅μ λ΄λ €μ€ - μ΄ν μμ²μμλ Request Cookie μμ
JSESSIONID
λ₯Ό μΆμΆνμ¬ κ²μ¦μ΄ μ ν¨νλ©΄ Authentication λ₯Ό μ 곡ν¨
μ°Έκ³ μλ£
μ€μ
1. μμ‘΄μ± μΆκ°
<!-- Properties -->
<security.version>4.2.7.RELEASE</security.version>
<!-- Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>${security.version}</version>
</dependency>
web.xml
μ€μ
2. <context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath:applicationContext.xml classpath:applicationContext-security.xml </param-value>
</context-param>
<!-- Spring Security -->
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<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>
- HttpSessionEventPublisher
- νλͺ μ μ¬μ©μκ° λ€λ₯Έ λΈλΌμ°μ λ‘ λμμ λ‘κ·ΈμΈ νλκ²μ λ§μ
- DelegatingFilterProxy
- λͺ¨λ μμ²μ ν΄λΉ νλ‘μ νν°λ₯Ό κ±°μΉλ€.
- Spring Security λ μ΄λ₯Ό ν΅ν΄ μΈμ¦ λ° μΈκ°λ₯Ό μννλ€.
3. security μ€μ
<security:http auto-config="true" use-expressions="true">
<security:csrf disabled="true"/>
<security:intercept-url pattern="/**" access="permitAll" />
<security:form-login login-page="/login" authentication-success-handler-ref="loginSuccessHandler" authentication-failure-handler-ref="loginFailureHandler" login-processing-url="/auth" username-parameter="id" password-parameter="pw" />
<security:logout logout-url="/logout" invalidate-session="true" logout-success-url="/login?status=logout" />
<security:session-management invalid-session-url="/login">
<security:concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
</security:session-management>
</security:http>
<!-- secured method -->
<security:global-method-security secured-annotations="enabled" />
<!-- provider -->
<security:authentication-manager>
<security:authentication-provider ref="userAuthHelper" />
</security:authentication-manager>
<bean id="loginSuccessHandler" class="com.devljh.domain.user.helper.LoginSuccessHandler">
<property name="defaultTargetUrl" value="/main" />
<property name="alwaysUseDefaultTargetUrl" value="true" />
</bean>
<bean id="loginFailureHandler" class="com.devljh.domain.user.helper.LoginFailureHandler">
<property name="defaultFailureUrl" value="/login?status=fail" />
</bean>
<bean id="userAuthService" class="com.devljh.domain.user.UserAuthService" />
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
<bean id="userAuthProvider" class="com.devljh.domain.user.helper.UserAuthProvider">
<property name="userDetailsService" ref="userAuthService" />
<property name="passwordEncoder" ref="passwordEncoder" />
</bean>