[英]How to integrate oAuth2.0 with Spring boot 1.5.8.RELEASE
[英]How to configure Spring Boot OAuth2.0 server with custom UserDetailsService and password validation?
我正在嘗試在現有架構上配置Spring Boot OAuth2.0服務器。 要使用我的模式對用戶進行身份驗證,我需要從我的users
表中檢索加密的密碼,並將其傳遞給外部服務以進行驗證。
我可以如下定義一個自定義UserDetailsService
,但是默認行為是根據返回的UserDetails
對象中的密碼和請求中的密碼進行比較來進行身份驗證的。
如何自定義此行為,以便可以使用自定義UserDetailsService
,但將密碼驗證卸載到外部服務?
@Component("userDetailsService")
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
@Autowired
private DataSource dataSource;
@Override
public UserDetails loadUserByUsername(final String login) {
String sql = "select encrypted_password from users where login_name = ?";
String encryptedPassword = null;
try {
Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement(sql));
statement.setString(1, login);
try (ResultSet results = statement.executeQuery()) {
if (results.next()) {
encryptedPassword = results.getLong("encrypted_password");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return new User(login, encryptedPassword, Arrays.asList(new SimpleGrantedAuthority("simpleAuth")));
}
}
我可能沒有足夠的細節可以解決您的問題,但是我會做一些假設,並為您提供最佳答案。
假設:用戶必須使用某種憑據登錄,然后再執行loadUserByUsername(loginCreds)
。
如果你不希望使用特定的UserDetails對象,你可以創建一個CustomAuthenticationFilter
是在WebSecurity類使用配置http.addFilterBefore(new CustomAuthenticationFilter(), UsernamePasswordAuthenticationFilter)
從那里,您可以自定義整個身份驗證流程。 您可以選擇創建一個CustomAuthenticationToken
,然后通過CustomAuthenticationProvider
進行身份驗證。 在此提供程序中,您可以使用第三方驗證進行密碼檢查。 如果通過,則可以在那里加載用戶對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.