I am trying to learn Spring security, and I got a problem.
I have a front-end project & a back-end projec.
Front-end project use node.js http-server on localhost:8000
Back-end project use Spring Boot Tomcat on localhost:8090
And I just add spring security to my back-end project.
Now the problem is I login in login.html, and redirect to index.html, but in index.html I want to access resource which in my back-end project, but Spring security always give me 401 error, if I type the url (for instance http://localhost://8090/getName), the browser would alter, and ask me to input username and password. After I finish, I type the other url (http://localhost://8090/getName2), I got the result directively. And it seems I didn't login. What's my problem?
Here is my Spring config:
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.httpBasic()
.and()
.authorizeRequests()
.anyRequest().authenticated()
;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
}
and this is my controller
@RequestMapping("/login")
String login(Principal principal){
return principal.getName();
}
and this is my fron-end login page's js,i use angular-js
loginApp.controller('navigation', function($rootScope, $http, $location, $scope, $window) {
$scope.login = function() {
var h = {authorization : "Basic "+ btoa($scope.credentials.username + ":" + $scope.credentials.password) };
$http.get('http://localhost:8090/login/',{headers : h}).then(function(response) {
onsole.log(response.data);
$window.location.href='/index.html';
});
};
});