Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 재귀함수 예제
- Katalon Recorder 사용법
- 주식 양도세 신고방법
- 최대공약수 예제
- 피보나치함수
- 재귀 예제
- 홈택스 해외주식 양도세
- 국세청 해외주식 양도세 신고방식
- 한국투자증권 해외주식 양도세
- 피보나치 예제
- recursion example
- katalon
- oracle group by
- 해외증권 양도세 한국투자증권
- java.sql.SQLSyntaxErrorException
- 피보나치함수 예제
- katalon 자동화
- 해외주식 양도세 신고
- bfs 미로탐색 java
- katalon 비교
- CSTS 폭포수 모델
- katalon 사용법
- 테스트 자동화
- javascript 자동완성
- tomcat log
- 한국투자증권 양도세 신고
- katalon xpath
- git 연동
- js 자동완성
- 톰캣 실시간 로그
Archives
- Today
- Total
엄지월드
Spring Security 설정 본문
Maven pom.xml에 스프링 시큐리티 추가
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
스프링 시큐리티 모든 페이지를 설정해주는 경우
package com.myapp.lms;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/**") // 모두 허용하겠다.
.permitAll();
super.configure(http);
}
}
특정 URL만 허용하는 경우
package com.myapp.lms;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(
"/",
"/member/register",
"/member/email-auth"
)
.permitAll();
super.configure(http);
}
}
ADMIN 처리 하는 방법
SecurityConfiguration 파일 내 아래 코드 추가
@Override
protected void configure(HttpSecurity http) throws Exception {
// ADMIN 페이지 설정. /admin/이 붙은 도메인은 모두 ADMIN 처리
http.authorizeRequests()
.antMatchers("/admin/**")
.hasAuthority("ROLE_ADMIN");
// 에러 페이지 핸들링
http.exceptionHandling()
.accessDeniedPage("/error/denied");
super.configure(http);
}
MemberServiceImpl.java의 loadUserByUsername에서 grantedAuthorities.add 추가
package com.myapp.lms.configuration;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
String msg = "로그인에 실패했습니다.";
if(exception instanceof InternalAuthenticationServiceException){
msg = exception.getMessage();
}else{
msg = exception.getMessage();
if(exception.equals("자격 증명에 실패하였습니다.")){
msg = "아이디나 비밀번호가 틀립니다.";
}
}
setUseForward(true);
setDefaultFailureUrl("/member/login?error=true");
request.setAttribute("errorMessage", msg);
System.out.println(msg);
super.onAuthenticationFailure(request, response, exception);
}
}
'java > Spring' 카테고리의 다른 글
Spring paging (0) | 2022.08.10 |
---|---|
thymeleaf fragment 설정 (0) | 2022.08.09 |
JPA (0) | 2022.08.04 |
Parameter 0 of constructor in com.myapp.lms.component.MailComponents required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found. 에러 (0) | 2022.08.03 |
@Valid import 에러 (0) | 2022.07.22 |
Comments