일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 소스트리
- html
- 리눅스
- 2차원배열
- 코딩테스트
- programmers
- 시큐리티
- Linux
- 싱글톤
- sql
- 프로그래머스
- 목록
- javascript
- 반복문
- 시큐리티 로그아웃
- gradle
- security
- 시큐리티로그인
- java
- input태그
- StyleSheet
- springboot
- codingtest
- 스프링 부트
- Spring boot
- 로그인
- springSecurity
- 시큐리티 로그인
- JAVA11
- css
Archives
- Today
- Total
JAVAIARY
Spring Security - 2. 로그인 처리 본문
0. 기본 페이지 생성
@Log4j
@RequestMapping("/sample/*")
@Controller
public class SampleController {
@GetMapping("/all")
public void doAll() {
log.info("do all can access everybody");
}
@GetMapping("/member")
public void doMember() {
log.info("logined member");
}
@GetMapping("/admin")
public void doAdmin() {
log.info("admin only");
}
}
1. 접근 제한 설정
security-context.xml
<security:http>
<security:intercept-url pattern="/sample/all" access="permitAll"/>
<security:intercept-url pattern="/sample/member" access="hasRole('ROLE_MEMBER')"/>
<security:form-login />
</security:http>
<security:authentication-manager>
</security:authentication-manager>
</beans>
- <security:intercept-url />
- <security:intercept-url pattern="/sample/member" access="hasRole('ROLE_MEMBER')"/>
- pattern : uri 패턴
- access : 권한 체크
- /sample/member URI 는 'ROLE_MEMBER'라는 권한이 있는 사용자만 접근 가능
- 권한이 없는 상태로 sample/member URI로 접속을 시도 하게 되면
- /login 으로 강제 이동됨 ( 스프링 시큐리티에서 기본제공해주는 로그인 폼)
2. 단순 로그인 처리
1) 로그인 확인을 위한 추가 설정
security-context.xml
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="member" password="{noop}member" authorities="ROLE_MEMBER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
- member라는 계정정보를 가진 사용자가 로그인을 할 수 있도록 하는 것
※ 주의사항 : 시큐리티에서는 username을 일반적인 id의 의미로 사용함. 혼동하지 않도록 주의할 것
- {noop} 없이 해당 정보로 로그인 시도시에는 PasswordEncoder가 없기 때문에 오류 발생
- id : member pw : member 로 로그인시 member page로 정상이동
2) admin 권한 사용자 설정
<security:user name="admin" password="{noop}admin" authorities="ROLE_MEMBER, ROLE_ADMIN"/>
- admin 계정을 가진 사용자에게는 ROLE_MEMBER와 ROLE_ADMIN 권한 2개를 모두 주어 모든 페이지에 접속이 가능하도록 한다.
- id: admin pw: admin 계정으로 접속하면 admin page와 member page에 접속이 가능한 것을 확인할 수 있음
2) 접근 제한 메시지 처리
- 당연하게도 member계정으로는 admin page에 접속할 수 없음
- AccessDeniedHandler를 구현하거나 URI 지정으로 처리할 수 있음
2-1) URI 지정을 통한 처리
<!-- 접근 거부 처리 -->
<security:access-denied-handler error-page="/accessError"/>
- /accessError 라는 URI로 접근제한시 보이는 화면 처리
@Log4j
@Controller
public class CommonController {
@GetMapping("/accessError")
public void accessDenied(Authentication auth, Model model) {
log.info("access Denied :" + auth);
model.addAttribute("msg", "접근 거부됨");
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<body>
<h1>Access Denied Page</h1>
<h2><c:out value="${SPRING_SECURITY_403_EXCEPTION.getMessage()}"/></h2>
<h2><c:out value="${msg}"/></h2>
</body>
- HttpServletRequest에 SPRING_SECURITY_403_EXCEPTION 라는 이름으로 AccessDeniedException객체 전달
- accessError 페이지로 보내지지만 URI는 접속한 URI 그대로임
2-2) AccessDeniedHandler (인터페이스) 구현을 통한 처리
- 다양한 처리 가능
- ex) 접근 제한시 쿠키or세션에 특정 작업하기/ HttpServletResponse에 특정 헤더 정보 추가 등
@Log4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler{
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException, ServletException {
log.info("Access Denied Handler");
log.error("Redirect....");
response.sendRedirect("/springsecurity/accessError");
}
}
- AccessDeniedHandler인터페이스를 구현 해준다.
- 위에서는 redirect하는 방식으로 동작하도록 설계
- customAccessDenied로 동작하도록 등록
- member/member 계정으로 sample/admin 에 접근했을 때 springsecurity/accessError 로 강제 이동됨
# 코드로 배우는 웹 프로젝트 참고
'lectureNote > SPRING' 카테고리의 다른 글
Spring Security - 4. 로그인 성공 / 로그아웃 (0) | 2022.12.18 |
---|---|
Spring Security - 3. 커스텀 로그인 페이지 & CSRF 토큰 (0) | 2022.12.16 |
Spring Security - 1. 환경설정 (0) | 2022.12.14 |
OAuth (0) | 2022.11.20 |
비즈니스 로직과 서비스 (0) | 2022.11.16 |