※ Spring Frame Work 3.2 기준 실제 사용 했던 Servlet.xml



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/task
        http://www.springframework.org/schema/task/spring-task-3.2.xsd">
        
 
 
 
    <!-- The controllers are autodetected POJOs labeled with the @Controller, @Service, @Component annotation. -->
    <!-- @Controller, @Service, @Component annotation 아래의 package를 스켄하여 @Autowired 
        어노테이션을 이용하여 해당하는 엔티티 및 서비스를 맵핑한다. -->
    <!-- <context:component-scan base-package="com.isource.web.controller, com.isource.domain.service, 
        com.isource.web.validator, com.isource.web.security"/> -->
        
        
        
        
    
    <context:component-scan base-package="com.myPackage.web" />
    <mvc:resources location="/WEB-INF/resources/" mapping="/resources/**" />
 
 
    <context:annotation-config></context:annotation-config>
<!--     <task:executor id="taskExecutor" pool-size="5"/>
    <task:annotation-driven executor="taskExecutor"/> -->
    
    
 
    <!-- Annotation 기반 트랜잭션 설정 -->
    <tx:annotation-driven transaction-manager="oracleTransactionManager" />
 
    <!-- 트랜잭션 매니저 -->
    <bean id="oracleTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/mybatis/mybatisConfig.xml" />
        <property name="mapperLocations" value="classpath:/mybatis/sql/**/*.xml" />
    </bean>
 
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    <bean id="sqlBatchSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <constructor-arg index="1" value="BATCH" />
    </bean>
 
    <bean id="anotherTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="anotherDataSource" />
    </bean>
 
    <bean id="anotherSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="anotherDataSource" />
        <property name="configLocation" value="classpath:/mybatis/mybatisConfig_another.xml" />
        <property name="mapperLocations" value="classpath:/mybatis/sql_another/**/*.xml" />
    </bean>
 
    <bean id="anotherSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="anotherSqlSessionFactory" />
    </bean>
    
    <bean id="anotherBatchSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="anotherSqlSessionFactory" />
        <constructor-arg index="1" value="BATCH" />
    </bean>
 
    <!-- Validation 활성화(validator="validator"), conversionService 활성화, 메세지 컨버터 
        등록 (JSON을 바꿔주는부분) -->
    <mvc:annotation-driven conversion-service="conversionService">
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.myPackage.web.converter.CustomObjectMapper" />
                </property>
            </bean>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
 
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/login**" />
            <mvc:exclude-mapping path="/login*" />
            <mvc:exclude-mapping path="/loginform**" />
            <mvc:exclude-mapping path="/resources/**" />
            <mvc:exclude-mapping path="/*.ico" />
            <bean class="com.isource.web.controller.IsourceInterceptor" />
        </mvc:interceptor>
 
        <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="convertLocale" />
        </bean>
    </mvc:interceptors>
    
    
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="ko" />
    </bean>
    
 
    <!-- Configure the multipart resolver -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- one of the properties available; the maximum file size in bytes about 
            10MB -->
        <property name="maxUploadSize" value="10000000" />
    </bean>
    <alias name="multipartResolver" alias="multipartResolver" />
 
    <!-- Configure the file upload directory -->
    <!-- <bean id="uploadDirResource" class="org.springframework.core.io.FileSystemResource"> 
        <constructor-arg> <value>C:/myPackage/upload/</value> </constructor-arg> </bean> -->
 
    <!-- tiles 2에서 전부처리되어 주석처리됨. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="2" p:prefix="/WEB-INF/views/" p:suffix=".jsp"/> -->
 
    <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" p:order="1" p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />
    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles/tiles-def.xml</value>
            </list>
        </property>
    </bean>
 
    <!-- @Valid 에서 스프링의 Message.properties를 사용하기 위해 추기 -->
    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource" />
    </bean>
 
    <!-- Message -->
    <bean id="messageSource"
        class="com.myPackage.extendz.IscReloadableResourceBundleMessageSource">
        <!-- class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> -->
        <property name="defaultEncoding" value="UTF-8" />
        <property name="basenames">
            <list>
                <value>classpath:/messages/messages</value>
                <value>classpath:/messages/buttons</value>
                <value>classpath:/messages/labels</value>
            </list>
        </property>
    </bean>
 
    <!-- Exception 처리 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
        <property name="order" value="1" />
    </bean>
    
    
    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="order" value="2" />
        <property name="defaultErrorView" value="/common/error500" />
        <!--.main 은 타일즈 standalone layout 적용 -->
    </bean>
 
 
    <!-- Conversion Service 기본 포멧터 사용 -->
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
 
    <bean id="logDataSource_pos" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
        <constructor-arg ref="dataSource" />
        <property name="logFormatter">
            <bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">
                <property name="loggingType" value="MULTI_LINE" />
                <property name="sqlPrefix" value="SQL=>" />
            </bean>
        </property>
    </bean>
 
 
    <!-- Quartz -->
    <bean id="batchService" class="com.myPackage.schedule.quartz.job.service.BatchService" />
    <bean id="sapJcoTransfer" class="com.myPackage.schedule.quartz.job.transfer.SapJcoTransfer" />
    <bean id="procedureTransfer" class="com.myPackage.schedule.quartz.job.transfer.ProcedureTransfer" />
    
 
     <bean name="isourceJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean" p:jobClass="com.isource.schedule.quartz.job.IsourceQuartzJobBean" p:durability="true">
        <property name="jobDataAsMap">
            <map>
                <entry key="batchService" value-ref="batchService" />
            </map>
        </property>
    </bean>
 
    <!-- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean" p:jobDetail-ref="isourceJob" p:startDelay="0" p:cronExpression="0/10 * * * * ?" /> -->
    
    <bean id="SimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="isourceJob" />
        <property name="startDelay" value="0" />
        <property name="repeatInterval" value="604800000" /
    </bean>
    
          
          
          
          
    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="SimpleTrigger" />
            </list>
        </property>
    </bean>
    <bean class="com.myPackage.common.util.ApplicationContextAwareExtends"/>
    
    
    
    <bean class="com.myPackage.common.helper.mail.MailSendWorker">
        <constructor-arg>
            <ref bean="naverMailSender" />
        </constructor-arg>
    </bean>
    
    <!-- Gmail setting -->
    <bean name="gmailMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
            p:password="input your password"
            p:host="smtp.gmail.com"
            p:port="587"
            p:protocol="smtp"
            p:username="input your account"
            p:defaultEncoding="utf-8">
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.ssl.enable">false</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.ssl.trust">*</prop>
                <prop key="mail.smtp.port">587</prop><!-- mail.smtp.ssl.enable true인 경우 주석처리후 사용 -->
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>
    
    <!-- naver setting -->
    <bean name="naverMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
            p:password="input your password" 
            p:host="smtp.naver.com"
            p:port="465"
            p:protocol="smtps" 
            p:username="input your account"
            p:defaultEncoding="utf-8">
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.ssl.enable">true</prop>
                <prop key="mail.smtps.ssl.checkserveridentity">true</prop>
                <prop key="mail.smtps.ssl.trust">*</prop>
                <prop key="mail.debug">false</prop>
            </props>
        </property>
    </bean>
    
    <!-- Local Stmp setting -->
    <bean name="customMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
            p:host="smtp.mailplug.co.kr"
            p:protocol="SSL"
            p:defaultEncoding="utf-8">
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.port">465</prop>
                <prop key="mail.smtp.starttls.enable">false</prop>
                <prop key="mail.smtp.ssl.enable">true</prop>
                <prop key="mail.smtp.ssl.trust">*</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
        <!-- <property name="username" value="customSTMPUser"/>
        <property name="password" value="customSTMPPassword"/> -->
 
    </bean>
    
 
</beans>
 
cs


개요 

SSL 인증서를 설치하고 난 뒤 성공하여 테스트를 진행할 경우 https를 붙여 테스트를 합니다.

하지만 여기서 우리가 간과한 것이 있습니다. 해당 페이지는 https로만 페이지가 열려야하는데 http로도 열리게 됩니다. 

이렇게 되어서는 인증서를 적용한 의미가 없지요 그래서 Spring-security의 일부 기능을 이용하여 지정된 페이지 혹은 URL Pattern에 따라 강제로 https로 열리게 할 수 있는 기능을 구현하도록 해봅시다.



준비물 


저는 구성되어 있는 환경이 Spring 3.2 이므로 Spring-security도 3.2로 구성하도록 하겠습니다. 


1. Spring Framework 3.2 환경

2. Spring-security 3.2 jar  ( 예제에 사용된 라이브러리 - spring-security-3.2.0.M1.zip


   (maven을 사용하지 않는경우는 블로그를 참조하여 파일을 준비합니다. http://cheezred.tistory.com/123)


환경 설정


우리가 작업할 파일은 수정할 web.xml 파일과 신규로 생성할 spring-security.xml 입니다. 

먼저 spring-security.xml을 먼저 생성합니다. 일반적으로 해당 파일의 위치는 Spring Framework 설정파일과 같은 위치에 생성합니다. 

- spring-security.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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="/resources/**" security="none" />
         
         <security:http auto-config="true">
             <security:intercept-url pattern="/**" requires-channel="https" />
         </security:http>
 
        <security:authentication-manager></security:authentication-manager>
 
</beans>
 
cs


>> Source Detail

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

=> url pattern이 /resources/로 시작되는 요청은 spring security 를 적용하지 않음


<security:intercept-url pattern="/**" requires-channel="https" />

=> 모든 요청에 대해서 https 통신을 적용하겠다 라는 내용입니다.

여러 패턴을 구성하여 http 및 https를 구분하여 페이지를 요청 할 수 있습니다.




- web.xml #1

1
2
3
4
5
6
7
8
9
10
11
<!-- Spring Security Start -->    
    <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>
<!-- Spring Security End -->
cs


>> Source Detail

위의 부분을 web.xml에 추가합니다. 주의할 점이 있는데 springSecurityFilterChain 이름을 변경하지 마시기 바랍니다. 클래스를 못찾는 오류가 발생합니다. 


- web.xml #2

1
2
3
4
5
6
7
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/datasource.xml
            /WEB-INF/spring/spring-security.xml
        </param-value>
</context-param
cs
>> Source Detail

위의 붉은색 항목을 web.xml에 추가합니다. 위에서 작성한 spring-security.xml을 WAS 기동 시 적용시키기 위함입니다. 

주의 할점은 web.xml에 DispatcherServlet 항목이 있는데 그곳에 추가하시는 것이 아니라는 것입니다. 

contextConfigLocation 항목이 없으신 경우 추가하셔서 붉은색 부분을 넣으시면 됩니다.

작성자는 contextConfigLocation를 사용하고 있어 붉은색 부분을 추가한 부분입니다.



테스트

- https로 호출되어야 하는 페이지를 http로 호출해 봅니다. https로 변환되어 페이지가 열리면 올바르게

설정되었다고 할 수 있습니다.




웹사이트에 Spring security에 대한 글이 많습니다. 아래 소개한 기능외에 많은 기능이 있습니다. 작성자는 https로 페이지를 분기하기 위한 기능만 사용한 예시 입니다.  해당 글들을 다수 정독하였지만 올바르게 작성된 글이 없어 실제 사용된 소스를 기반으로 작성되었습니다. 

'Development > Java' 카테고리의 다른 글

[Mybatis] java.lang.NumberFormatException 발생  (0) 2017.04.20
[Quartz] 1.x.x 사용하다 2.x.x로 변경 시 변경된 메서드  (0) 2017.03.16
Jar File Download Site  (2) 2016.12.20
Spring과 Quartz 연동  (0) 2016.10.27
Java Locale List  (0) 2016.08.31

+ Recent posts