티스토리 뷰
728x90
반응형
AuthenticationProvider를 구현함으로써 인증 공급자를 재구성할 수 있다.
package com.example.demo.config;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private final UserDetailsService userDetailsService;
private final PasswordEncoder passwordEncoder;
public CustomAuthenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
this.userDetailsService = userDetailsService;
this.passwordEncoder = passwordEncoder;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String userName = authentication.getName();
String userPassword = String.valueOf(authentication.getCredentials());
UserDetails userDetails = this.userDetailsService.loadUserByUsername(userName);
if(this.passwordEncoder.matches(userPassword, userDetails.getPassword())) {
return new UsernamePasswordAuthenticationToken(userName, userPassword, userDetails.getAuthorities());
} else {
throw new BadCredentialsException("Invalid username or password");
}
}
@Override
public boolean supports(Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
}
AuthenticationProvider를 구현한 "CustomAuthenticationProvider"를 스프링 시큐리티에 추가한다.
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {
@Bean
UserDetailsService userDetailsService() {
var userDetailsService = new InMemoryUserDetailsManager();
var user = User.withUsername("james")
.password(passwordEncoder().encode("12345"))
.authorities("auth_name")
.build();
userDetailsService.createUser(user);
return userDetailsService;
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic(Customizer.withDefaults()) // HTTP Basic 인증 활성화
.authorizeHttpRequests(authorize -> authorize
//.anyRequest().permitAll() // 인증 없이 접근 가능
//.requestMatchers("/hi").permitAll() // "/hi"만 인증 없이 접근 가능
.anyRequest().authenticated() // 인증 필수("/hi"는 제외)
);
return http.build();
}
@Bean
CustomAuthenticationProvider customAuthenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
return new CustomAuthenticationProvider(userDetailsService, passwordEncoder);
}
}
728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- error-java
- In App Purchase
- jstl(java standard tag library)
- jsp 오픈 소스
- React
- .submit()
- 스프링 프레임워크(spring framewordk)
- 특정 문자를 기준으로 자르기
- System.Diagnostics
- 제품 등록
- await
- jstl(java standard tag library)-core
- java-개발 환경 설정하기
- 메이븐(maven)
- java 키워드 정리
- 표현 언어(expression language)
- 스프링 시큐리티(spring security)
- MainActor
- 람다식(lambda expression)
- 상품 등록
- nl2br
- 인텔리제이(intellij)
- system.io
- 문자 자르기
- 진수 변환
- 스프링 프레임워크(spring framework)
- REST API
- java web-mvc
- 스프링 시큐리티(spring security)-http basic 인증
- java.sql
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함