spring
Spring Web Security 설정
Grindman
2020. 10. 22. 23:02
1. pom.xml의 일부
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.3.5.RELEASE</version>
</dependency>
2. security-context.xml 생성
2-1. Spring Bean Configuration File을 통해 생성
2-2. 네임스페이스에서 security 항목 체크
주의할점은
요놈을
요렇게 바꿔줘야한다. 5.0 네임스페이스에서 문제가 발생한단다.
3. web.xml 설정
스프링 시큐리티가 스프링 MVC에서 사용되기 위해서는 필터를 이용해서 스프링 동작에 관여하도록 설정한다.
web.xml일부
<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>
security-context.xml 일부..
<security:http>
<security:form-login/>
</security:http>
<security:authentication-manager>
</security:authentication-manager>
4. 시큐리티가 필요한 URI 설계
URI 설계를..
/sample/all -> 로그인을 하지 않은 사용자도 접근 가능한 URI
/sample/member -> 로그인 한 사용자들만이 접근할 수 있는 URI
/sample/admin -> 로그인 한 사용자들 중에서 관리자 권한을 가진 사용자만이 접근할 수 있는 URI
SampleController.java
package com.grindman.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/sample/*")
@Controller
public class SampleController {
@GetMapping("/all")
public void doAll() {
System.out.println("do all can access everybody");
}
@GetMapping("/member")
public void doMember() {
System.out.println("logined member");
}
@GetMapping("/admin")
public void doAdmin() {
System.out.println("admin only");
}
}
view폴더 아래 sample 폴더 작성, 해당 URI에 맞는 화면 작성