Spring Security is a powerful and highly customizable authentication and access-control framework. (스프링 시큐리티는 강하고 커스터마이징 가능한 인가처리 프레임워크다.) It is the de-facto standard for securing Spring-based applications. (이것은 스프링 베이스 어플리케이션에 사실상 표준으로 사용되고 있다.)
Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
i18n
디렉토리 아래, 다국어 처리를 위한 message property
static
아래에 asserts
, 다시 icons
을 만들고 favi.png
을 하나 넣어준다.application.yml
을 생성해줌. logback.yml
생성.configures
패키지 생성spring MVC
관련설정Spring Security Cibfugures
생성@Configuration
@EnableWebSecurity
public class WebSecurityConfigure extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/assets/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/me").hasAnyRole("USER", "ADMIN")
.anyRequest().permitAll()
.and()
.formLogin()
.defaultSuccessUrl("/")
.permitAll()
;
}
}
HttpSecurity
클래스는 세부적인 웹 보안기능을 설정을 처리할 수 있는 API를 제공.antMatchers("/me").hasAnyRole("USER", "ADMIN")
> "/me"라는 패스의 경우, 요청을 하는 사용자가 "USER", "ADMIN"라는 권한을 갖고 있어야한다. 인증영역이라는 것. .anyRequest().permitAll()
> 외의 모든 요청에 대해서 permitAll()한다. 익명영역.spring:
application:
name: spring security 01
thymeleaf:
cache: true
security:
user:
name: user
password: user123
roles: USER
messages:
basename: i18n/messages
encoding: UTF-8
cache-duration: PT1H
server:
port: 8080
http://www.thymeleaf.org/extras/spring-security
를 추가해줘야한다.<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<div th:text="${#authentication.name}">
The value of the "name" property of the authentication object should appear here.
</div>
<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>
<div sec:authentication="name">
The value of the "name" property of the authentication object should appear here.
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>