[英]Spring Security AngularJS Login
I have a Spring Boot application and configured spring security to it, it uses a custom login page with a simple AngularJS form and i'm trying to authenticate through it but unsuccessfully. 我有一个Spring Boot应用程序,并为其配置了spring安全性,它使用具有简单AngularJS表单的自定义登录页面,并且我尝试通过它进行身份验证,但未成功。 On my CurrentUserDetailsService loadUserByUsername(String param) the parameter
param
comes null everytime, can someone help me ? 在我的CurrentUserDetailsService loadUserByUsername(String param)上,参数
param
每次都为null,有人可以帮助我吗? am i missing something? 我想念什么吗?
This is my form : 这是我的表格:
<form role="form" action="" name="form" class="">
<fieldset class="pure-group">
<legend>Log In</legend>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username" ng-model="credentials.username"/>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" ng-model="credentials.password"/>
</div>
<button ng-click="onLogin($event)" type="submit" class="btn btn-primary">Submit</button>
</fieldset>
</form>
This is my js: 这是我的js:
$scope.onLogin = function($event){
$event.preventDefault();
$http({
method: 'POST',
url:'/login',
data: $scope.credentials
})
}
This is my security configuration : 这是我的安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/newuser","/create").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/**").hasRole("USER").anyRequest().authenticated()
.and()
.formLogin()
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/index.html")
.loginPage("/loginpage.html")
.and()
.httpBasic()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/loginpage.html")
.permitAll()
.and()
.csrf().csrfTokenRepository(csrfTokenRepository())
.and()
.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class);
}
and this is the exception i'm getting everytime: 这是我每次都得到的例外:
org.springframework.security.authentication.InternalAuthenticationServiceException: Cannot pass null or empty values to constructor
Also would like to mention that through my POST request username and password are sended 还想提到通过我的POST请求发送的用户名和密码
I tried with j_username and j_password but nothing. 我尝试使用j_username和j_password,但是什么也没有。
Try building your post data like this instead: 尝试像这样构建您的帖子数据:
$scope.onLogin = function($event){
$event.preventDefault();
var data = 'username=' + encodeURIComponent($scope.credentials.username) +
'&password=' + encodeURIComponent($scope.credentials.password) +
'&submit=Login';
$http.post('/login', data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}
That way it will be the expected Content-Type and if I'm not mistaken it matches the way the form would be posted normally. 这样,它将是预期的Content-Type,并且如果我没有记错的话,它将与表单正常发布的方式匹配。 Sorry for not elaborating more.
对不起,我没有详细说明。
Seems like you are posting the data as JSON instead of form_data (single params username and password). 似乎您将数据发布为JSON而不是form_data(单个参数用户名和密码)。 Please see this answer for details:
请查看此答案以获取详细信息:
How do I POST urlencoded form data with $http in AngularJS? 如何在AngularJS中使用$ http发布urlencode表单数据?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.