[英]SpringSecurity - SecurityConfiguration.java - HttpSecurity - not working
[英]Is it possible to login using the Spring SecurityConfiguration HttpSecurity without a password?
我知道我的問題有點奇怪,但是在連接到LDAP服務器的應用程序中,我們只需要讓用戶登錄到該應用程序即可。
該應用程序是Spring AngularJS。
所以現在我正在更改代碼以執行此操作,但是我發現登錄方法已完成到Spring安全配置中並使用了密碼,因此我需要從數據庫( user.getPassword()
)獲取密碼,問題是密碼是使用PasswordEncoder加密的,因此無法解密(在該答案中的答案中提到)。
無論如何,我想知道是否有一種方法可以僅使用userName(沒有密碼)登錄Spring Security。
我試圖從配置類中刪除密碼聲明,但是它不起作用。
在名為( public class SecurityConfiguration extends WebSecurityConfigurerAdapter
)的public class SecurityConfiguration extends WebSecurityConfigurerAdapter
我具有下一個配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.rememberMe()
.rememberMeServices(rememberMeServices)
.rememberMeParameter("remember-me")
.key(jHipsterProperties.getSecurity().getRememberMe().getKey())
.and()
.formLogin()
.loginProcessingUrl("/api/authentication")
.successHandler(ajaxAuthenticationSuccessHandler)
.failureHandler(ajaxAuthenticationFailureHandler)
.usernameParameter("j_username")
.passwordParameter("j_password")
.permitAll()
.and()
.logout()
.logoutUrl("/api/logout")
.logoutSuccessHandler(ajaxLogoutSuccessHandler)
.permitAll()
.and()
.headers()
.frameOptions()
.disable()
}
這是登錄方法:
function login (credentials) {
alert("log in this");
var data = 'j_username=' + encodeURIComponent(credentials.username)
+'&j_password=' + encodeURIComponent(credentials.password) +
'&remember-me=' + credentials.rememberMe + '&submit=Login';
return $http.post('api/authentication', data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function (response) {
return response;
});
}
有想法嗎?
謝謝你
您始終可以在應用程序中的任何時候創建SecurityContext
,無論它是否具有密碼都沒有關系:
Authentication authentication =
new UsernamePasswordAuthenticationToken(user, null, authorities);//set with authorities
SecurityContextHolder.getContext().setAuthentication(authentication);
與此相關的是,實際上,Spring會通知您您已經有registered
用戶。
我不確定您如何檢索用戶詳細信息,但是也許您可以只使用j_username作為用戶名和密碼,然后在檢索用戶詳細信息的方法中將其忽略即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.