요즘 프로젝트를 하고 있는데, 역시나 자바 시스템이 있다. Spring Boot3 을 사용하고 있고 자바 17을 쓰는등 나름대로 괜찮은 환경에서 개발이 이루어지고 있다. 그런데, 이것을 서버에서 배포를 하고 Spring Boot 를 실행해야 하는데, 어떻게 하나 봤더니 초보자 수준도 못 벗어나는 설정을 하고 있으니… 안타까운 마음에 어떻게 하는 것이 좋은 것인지 한번 적어봤다. Spring boot, jar 실행 파일 Spring Boot3 를 컴파일 하면 jar 파일 나온다. 그리고 별다른 서버 없이도 바로 실행하고 접속이 가능해 진다. 한가지 재미있는 사실은 많은 사람들이 Spring boot3 […]
Spring5 Security 기초 템플릿 – Session
이 글은 Spring5 에 Spring-Security 에 대한 기초 템플릿이다. Session 유지를 위해서 Redis 를 필요로 한다. 또, ElasticSearch 의 RestHighLevelClient 로 ElasticSearch를 연결 한다. 데이터베이스는 JNDI 설정으로 연결 된다. 다음과 같은 내용을 담았다. JDK 11 Tomcat 서버 9 를 이용. spring-session.xml 을 작성해 Redis 를 세션으로 사용하도록 했다. JNDI 를 이용해 MySQL 에 연결된다. Redis 설정한 이유는 Spring Security 에 인증을 InMemory 를 요구 한다. 이는 다음과 같은 설정 때문이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<authentication-manager> <authentication-provider> <user-service> <!-- This is required a InMemory DB. So I setup Redis and config to a spring-session.xml for connecting the redis if you set spring-session.xml, you should config also web.xml for springSessionRepositoryFilter --> <user name="jimi" password="{bcrypt}$2a$10$ddEWZUl8aU0GdZPPpy7wbu82dvEw/pBpbRvDQRqA41y6mK1CoH00m" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="bob" password="{bcrypt}$2a$10$/elFpMBnAYYig6KRR5bvOOYeZr1ie1hSogJryg9qDlhza4oCw1Qka" authorities="ROLE_USER" /> <!-- Password is prefixed with {noop} to indicate to DelegatingPasswordEncoder that NoOpPasswordEncoder should be used. This is not safe for production, but makes reading in samples easier. Normally passwords should be hashed using BCrypt --> <!-- <user name="jimi" password="{noop}jimispassword" authorities="ROLE_USER, ROLE_ADMIN" /> --> <!-- <user name="bob" password="{noop}bobspassword" authorities="ROLE_USER" /> --> </user-service> </authentication-provider> </authentication-manager> |
인증을 위한 패스워드가 {bcrypt} 로 되어 있다. 이 설정은 […]
Spring5 Security 기초 템플릿
이 문서는 Spring5 Security 기초 템플릿에 대한 것이다. Spring5 에 Spring-Security 를 이용해 기초적인 로그인을 구현 했다. Session 은 InMemory 가 아닌 WAS 서버에 세션을 저장한다. 로그인 정보는 spring-security.xml 에 정의된 것을 그대로 사용했다. 그야말로 기초적인 내용만 담았다. 이 템플릿이 동작하기 위한 조건은 다음과 같다. Java 1.8 기반. 데이터베이스가 연결되어 있어야 한다. 데이터베이스는 MySQL 이다. WAS 서버에 JNDI 를 연결된다. users 테이블이 필요한데, user.sql 파일에 내용이 있다. spring security 의 Role 기반 권한 접근 제어. context root 는 malluser 이다. […]
Mybatis에 DAO 와 Mapper
SpringFramework 을 이용하다보면 데이터베이스 액세스를 위해서 MyBatis 를 사용하곤 한다. SQL 매퍼라고 불리기도 하는 것인데, 이를 이용하면 손쉽게 자바 코드와 SQL 문을 분리해줄 수 있을뿐만 아니라 MyBatis 에서 제공하는 여러가지 추가적인 기능을 이용해 데이터베이스를 좀 더 유연하게 사용할 수 있다. DAO 보통 MyBatis 를 이용할대는 DAO 를 구조를 사용하곤 했다. Data Access Object 라고 불리는 것으로 말 그대로 데이터 접근을 위한 객체로서 sqlSession 객체를 이용해 데이터베이스 조회만 전담하는 객체다. 이 구조는 인터페이스와 그것을 구현한 구현체 클래스가 있어야 한다. 이를 위해서 […]
HTTP 응답 헤더 보안
의외로 웹 프로그래밍을 하는 사람들 조차도 HTTP 응답 헤더에 대해서 생각을 하지 않는다. HTTP 응답 헤더에 대한 생각을 하지 않는다는 것은 웹 프로그래밍을 깊게 공부한 사람이 아니라고 말할 수도 있다. Cache Control 대부분의 사람들은 이 부분에 대해서는 잘 안다. Cache Control 헤더는 Client 에 컨텐츠를 어떻게 캐시할 것인지에 대한 제어를 할 수 있다. 하지만 고려해야할 사항이 존재한다. 만약 AWS Cloud 를 사용한다면 더 더욱 고려해야할 사항이 존재한다. AWS 의 CloudFront 는 CDN 서비스로서 컨텐츠 캐시를 기반으로 한다. 만일 특정 컨텐츠에 […]
Spring5 requirements
Spring5 를위한 필요사항들을 정리. java 8. 원래 Spring5 는 Java 9를 기반으로 하려고 했지만 변경됐다. 이로 인해서 Reactive Programming을 하기 위해서는 의존성 라이브러리를 필요로 한다. 현재Java 9 은 지원이 중단된 상태다. Java 10 을 사용해도 된다. Java EE 8 호환. Servlet 4.0, Bean Validation 2.0 등을 지원한다. HTTP/2 지원한다. Jackson 2.9, Protobuf 3.0 지원.
MyBatis Error – The content of elements must consist of well-formed character data or markup.
MyBatis 의 Mapper XML 파일에서 다음과 같은 오류가 발생 했다. “The content of elements must consist of well-formed character data or markup.” 이는 SQL 연산자인 <, =, > 와 같은 엔터티가 MyBatis 문법과 혼동되서 나오는 문제다. 이럴때는 CDATA 를 적용해주면 된다.
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 |
<select id="getCheckInDay" resultType="CheckInDayVO"> <![CDATA[ SELECT dayofweek, DATE(start_date) AS day, start_date, end_date, TIMEDIFF(end_date, start_date) AS worktime, CASE WHEN end_date is null OR TIMESTAMPDIFF(MINUTE,start_date,end_date) < 160 THEN TIMESTAMPDIFF(MINUTE,start_date,now()) ELSE TIMESTAMPDIFF(MINUTE,start_date,end_date) END workminute, CASE WHEN end_date is null OR TIMESTAMPDIFF(MINUTE,start_date,end_date) < 160 THEN (TIMESTAMPDIFF(MINUTE,start_date,now())/480)*100 ELSE (TIMESTAMPDIFF(MINUTE,start_date,end_date)/480)*100 END workpercentage FROM checkin WHERE username = #{username} AND DATE(start_date) = #{today} ]]> </select> |
이렇게하면 에러를 없앨 수 있다.
스프링 디렉토리 구조 재정의하기
이클립스(Eclipse)에서 STS 를 설치하면 아주 간단하게 Spring MVC 샘플 프로젝트를 생성할 수 있다. 이 샘플 프로젝트의 디렉토리는 다음과 같다. 엄밀히 말하면 사실 이 디렉토리 구조는 Maven 이 관리 한다. Maven 에서는 기본적으로 Java 소스 디렉토리, Resource 디렉토리, webapp 디렉토리를 위와 같이 정의해 놓고 있다. STS 의 Spring MVC 샘플 프로젝트도 Maven을 기반으로 만들어 졌기 때문에 위와 같은 디렉토리 구조를 가진다. 하지만 한국에서는 다음과 같은 디렉토리 구조를 주로 사용한다. webapp 의 루트(root) 디렉토리를 WebContent 로 바꾸고 스프링의 설정 파일인 root-context.xml, servlet-context.xml […]
web.xml 스키마 예제 헤더
web.xml 스키마 헤더 예제는 버전별로 다음과 같다. Servlet 2.5
1 2 3 4 |
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="2.5"> |
Servlet 3.0
1 2 3 4 |
<?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0"> |
Servlet 3.1
1 2 3 |
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> |
Servlet 4.0
1 2 3 4 5 6 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> |
Servlet 5.0
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> <!-- Config here. --> </web-app> |
Servlet 6.0
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0"> <!-- Config here. --> </web-app> |
참고: IBM Knowledge Center
스프링 3 입문 예제소스
스프링 3입문 예제소스 입니다. 스프링 3 입문 예제소스 다음의 주소에서도 구할 수 있습니다. 한빛출판네트워크