[eclipse] checkstyle error : expecting EOF found '{'

Java 1.8에서 Checkstyle을 사용할 때, 람다 표현을 쓰면 expecting EOF found '{' 와 같은 에러가 난다.

Checkstyle 5.9 버전부터 자바 8을 지원한다고 하니 업그레이드 하고 나서 refresh 하면 된다.


Reference : 

https://jira.codehaus.org/browse/MCHECKSTYLE-253

[maven] Profile로 설정파일 나누기

개요

로컬에서 상용되는 데이터베이스에 붙는 일은 거의 없을 것이다. 로컬에서는 테스트용 데이터베이스에 연결되고, 실서버에서는 리얼 데이터베이스에 연결하면서 소스는 수정되지 않길 원한다. 그래서 설정파일로 빼고, 예전에는 수동으로 분기해주는 코드를 만들었는데, 이제는 메이븐에서 profile을 사용하면 간단하게 해결된다.
profile은 로컬과 리얼의 설정파일 뿐만 아니라 dependency도 분기할 수 있게 해준다.
예제에서는 설정파일만 나눠보자.

예제

먼저 메이븐 프로젝트를 생성하고, src/main 밑에 resource-local과 resource-real 폴더를 생성하자.

다음 pom.xml 을 아래와 같이 수정해준다.



pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tistory.jekalmin</groupId>
  <artifactId>profile-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <environment>local</environment>
        </properties>
    </profile>
    <profile>
        <id>real</id>
        <properties>
            <environment>real</environment>
        </properties>
    </profile>

  </profiles>

  <build>
    <resources>
        <resource>
            <directory>src/main/resource-${environment}</directory>
        </resource>
    </resources>
  </build>
</project>

수정한 후에,

프로젝트 우클릭 -> Maven -> Update Project를 실행하자.
실행하고 나면 resource-local이 소스폴더로 추가될 것이다.

이제 resource-local과 resource-real에 같은 파일명을 사용하더라도, 파일 안에서 다른 설정이 가능하다.
예를 들어 war로 리얼 설정을 담아서 만들고 싶다면,

mvn -Preal clean package

이렇게 사용하면 된다.

ps : 이유는 잘 모르겠으나 clean을 안하면 자꾸 local 설정 파일이 포함됐다. 나중에 원인을 알게되면 수정해야겠다.

[spring security] SecurityContext 가져오기

SecurityContext가 생성되는 시점은 시큐리티가 생성한 필터들 중에 두 번째 쯔음 호출되는 SecurityContextPersistenceFilter 필터 안에서 HttpSessionSecurityContextRepository 클래스를 사용하여 생성한다.
그래서 그 필터를 지난 이후에는 SecurityContextHolder에서 가져올 수 있다.

SecurityContext securityContext = SecurityContextHolder.getContext();

하지만 그 필터보다 더 앞에 호출되는 필터에서는 SecurityContext가 생성되기 전이기 때문에 SecurityContextHolder에서 가져올 수 없다.
그래도 결국 SecurityContext도 세션에 저장되기 때문에, 아래와 같이 세션에서 직접 가져올 수 있다.

Object securityContextObject =  session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
if(securityContextObject != null){
    SecurityContext securityContext = (SecurityContext)securityContextObject;
}

Session에 저장되는 키는 “SPRING_SECURITY_CONTEXT”인데, 이 키는 HttpSessionSecurityContextRepository 클래스에 정의 되있다.