inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 시큐리티

2) 관리자 시스템 - 권한 도메인, 서비스, 리포지토리 구성

권한에 따른 로그인 방식을 다르게 하는 방법

609

김유진

작성한 질문수 12

0

안녕하세요 선생님,

제가 구현하고자하는 부분은 

admin 권한을 가지고 있을 때 그냥 일반 아이디와 패스워드를 이용해서 바로 로그인 처리가 되도록 하고 싶고,

user나 manager 같은 권한은 otp 를 통해서 로그인이 되도록 구현하고 싶습니다.

그러려면 admin 권한은 BeforeAuthenticationFilter에서

if(custom !=null & passwordEncoder.matchest(password, real_password)) 에서 

권한이 admin 이면 바로 LoginSuccessfulHandler로 가도록 하고 싶은데 어떻게 리턴값을 줘야 할지 모르겠습니다.

참고로 otp 가 필요하다면 return super.attemptAuthentication(request, response) 로 가서 failurehandler로 가도록 구현해보았습니다.

spring-boot java Spring Security

답변 2

0

정수원

리턴타입을 보시면 아시겠지만 인증에 성공하면 스프링 시큐리티가 최종적으로 UsernamePasswordAuthenticationToken 을 반환합니다.

그렇기 때문에 강제로 인증에 성공할려고 하면

if(custom !=null & passwordEncoder.matchest(password, real_password)) {

    UsernamePasswordAuthenticationToken authResult
                                 = new UsernamePasswordAuthenticationToken(principal, credentials, authorities)      

return authResult;

}

이렇게 직접 UsernamePasswordAuthenticationToken 을 생성해서 인증에 성공했다고 가정하고 principal, credentials, authorities 에 해당하는 적절한 값을 채워서 리턴하게 되면 이후 처리는 스프링 시큐리티가 진행할 것 같습니다.

소스를 실행해 보지 못해서 예측한대로 흘러갈지 잘 모르겠습니다.

일단 시도해 보시고 오류가 발생하면 질문 남겨 주시기 바랍니다.

0

정수원

혹시 소스 공유 가능할까요?

0

김유진

public class BeforeAuthenticationFilter extends UsernamePasswordAuthenticationFilter에서

이렇게 구현해보았습니다. 아래 주석처리 된 부분에 리턴 값을 어떻게 줘야 할지 모르겠습니다 ㅠㅠ

    @Override
public Authentication attemptAuthentication(
HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
String email = request.getParameter("email");
String password = request.getParameter("password");

Account customer = userRepository.findByEmail(email);
String real_password = userRepository.findPasswordByEmail(email);

if (customer !=null & passwordEncoder.matches(password, real_password)) {
// System.out.println("customer.getRole ========"+userroles);
// if (userroles.equals("27")) {
//
// return attemptAuthentication(request,response);
// } else {

if (customer.isOTPRequired()) {
return super.attemptAuthentication(request, response);
}

float spamScore = getGoogleRecaptchaScore();

if (spamScore < 0.5) {
try {
customerService.generateOneTimePassword(customer);
throw new InsufficientAuthenticationException("OTP");
} catch (MessagingException | UnsupportedEncodingException ex) {
System.out.println("에러1" + ex);
throw new AuthenticationServiceException(
"Error while sending OTP email.");
}

}
}
// }

return super.attemptAuthentication(request, response);
}

시큐리티 공부 버전 질문

0

173

1

[해결 방법] MethodSecurityConfig.customMethodSecurityMetadataSource() 호출하지 않는 이슈

0

183

1

AbstractSecurityInterceptor.class.beforeInvocation()를 2번 실행하는 경우

0

172

1

강의 코드가 왜이렇게 뒤죽박죽인가요...

0

247

1

메인 페이지로 접속해도 login url로 리다이렉트가 되지 않습니다..

0

233

1

파라미터값이 넘어가지 않습니다 ....

0

372

1

security filterChain 설정 질문이 있습니다.

0

328

1

소스 부분 질문 드립니다.

0

206

2

섹션4 7번 강의 문제가 있는거 같네요.

0

342

2

파일이 수시로 이름이 바껴있네요 ㄷㄷ

0

302

1

HttpSessionSecurityContextRepository를 사용안하는 문제

0

553

2

error , exception 이 잘 안됩니다.

0

275

2

thymeleaf tag 질문합니다.

0

194

2

버전업하면서 deprecated된 것들이 너무많아요

0

476

1

spring security 패치 관련

0

435

1

모바일을 사용할때 토큰말고 세션

0

843

2

DB 연동한 인가 부분에 대한 질문입니다!

0

262

1

Ajax방식도 똑같이 Session방식을 사용하는건가요?

0

305

1

Config 파일 생성 시 질문이 있습니다.

0

223

1

강사님 몇일동안 구글 검색만 100개 했는데도 이유를 모르겠습니다..

1

427

2

403 에러 뜹니다.

0

810

2

login_proc의 존재에 대한 간략한 설명입니다

0

272

1

top.html에 로그인 링크를 만들어서 로그인을 해봤습니다

0

275

2

안녕하세요. DB에 저장될 때 이해 안 가는 값이 있어서 질문드립니다!

0

186

1