Jasyspt를 사용하여 중요한 데이터들을 암호화하기 위해서 먼저 해당 설정을 먼저 jasypt를 추가하여 설정을 진행해준다.
implementation group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.5'
해당 부분은 여기에서 가져올 수 있다.
등록되있는 gradle
해당 git
jasypt config 추가
@Configuration
@EnableEncryptableProperties
public class JasyptConfigDES {
@Value("${jasypt.encryptor.password}")
private String encryptKey;
@Bean(name = "jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(encryptKey);
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
return encryptor;
}
}
문제 발생
jasypt 암호화 작업이 제대로 안되서 username을 인지 못하는 문제 발생.
3.0.0 버전 부터 PBEWithMD5AndDES
→ PBEWITHHMACSHA512ANDAES_256
로 알고리즘 변경이 됨
PBEWithMD5AndDES
를 사용하려면 해당 부분을 바꿔줘야함.
config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
이부분을 2시간여 정도 헤맸는데 여러 블로그를 뒤져보니 해답이 나오긴 했다. -> 꼭 문서를 제대로 확인하자.
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
카카오 rest api 문서에 필요한 데이터를 yml 에 등록한다.
security:
oauth2:
client:
registartion:
kakao:
clientId: ENC(CeUq2FUb7sCJtQ/UPCVqNKcZ1OHdIAE6zi9u99kVjZ7F8p5owXBJXQy3+KG6G/9r)
scope:
client-name: Kakao
authorization-grant-type: authorization_code
redirect-uri: http://localhost:8080/login/oauth2/code/kakao
client-authentication-method: POST
provider:
kakao:
authorization-uri: https://kauth.kakao.com/oauth/authorize
token-uri: https://kauth.kakao.com/oauth/token
user-info-uri: https://kapi.kakao.com/v2/user/me
user-name-attribute: id
springSecurity에 oauthlogin 시도시 해당 위치 접근 허용
.oauth2Login(
oauth2Login -> oauth2Login
.loginPage("/member/login")
).
oauth로 처리된 데이터를 받아줄 UserServiceOauth 버전을 추가
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Slf4j
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
private final MemberService memberService;
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(userRequest);
String oauthId = oAuth2User.getName();
String providerTypeCode = userRequest.getClientRegistration().getRegistrationId().toUpperCase();
String username = providerTypeCode + "__%s".formatted(oauthId);
Member member = memberService.whenSocialLogin(providerTypeCode, username).getData();
return new CustomOAuth2User(member.getUsername(), member.getPassword(), member.getGrantedAuthorities());
}
}
class CustomOAuth2User extends User implements OAuth2User {
public CustomOAuth2User(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
@Override
public Map<String, Object> getAttributes() {
return null;
}
@Override
public String getName() {
return getUsername();
}
}
public RsData<Member> whenSocialLogin(String providerTypeCode, String username) {
Optional<Member> findMember = findByUsername(username);
if(findMember.isPresent()){
return RsData.of("S-1","로그인 되었습니다.", findMember.get());
}
return join(providerTypeCode,username,"");
}
There was an unexpected error (type=Internal Server Error, status=500).
This class supports `client_secret_basic`, `client_secret_post`, and `none` by default. Client [kakao] is using [POST] instead. Please use a supported client authentication method, or use `setRequestEntityConverter` to supply an instance that supports [POST].
java.lang.IllegalArgumentException: This class supports `client_secret_basic`, `client_secret_post`, and `none` by default. Client [kakao] is using [POST] instead. Please use a supported client authentication method, or use `setRequestEntityConverter` to supply an instance that supports [POST].
로그인 인증 과정 문제에 대해서 찾아보니 Spring Security 5.8 이상버전에서는
client-authentication-method: client_secret_post
OAuth Migrations :: Spring Security
암호화 방식 참고 블로그
oauth2 로그인 사용하기 :
https://deeplify.dev/back-end/spring/oauth2-social-login
해당 api 사용 방법 설명
: https://developers.kakao.com/docs/latest/ko/kakaologin/rest-api