springboot&x2B;angularjs JWT认证
我在生成jwt令牌时遇到问题。我用的是Springboot和AngularJs。我花了很多时间寻找解决方案,但什么也没找到。我还想问(如果可以的话),我如何在应用程序中保存令牌。我应该使用$localStorage或$Window还是其他什么springboot&x2B;angularjs JWT认证,angularjs,spring-boot,jwt,Angularjs,Spring Boot,Jwt,我在生成jwt令牌时遇到问题。我用的是Springboot和AngularJs。我花了很多时间寻找解决方案,但什么也没找到。我还想问(如果可以的话),我如何在应用程序中保存令牌。我应该使用$localStorage或$Window还是其他什么 @RequestMapping(value = "/user-login") public ResponseEntity<Map<String, Object>> login(@RequestParam
@RequestMapping(value = "/user-login")
public ResponseEntity<Map<String, Object>> login(@RequestParam String email, @RequestParam String password) throws IOException {
String token = null;
User appUser = userRepository.findByEmail(email);
Map<String, Object> tokenMap = new HashMap<String, Object>();
if (appUser != null && appUser.getPassword().equals(password)) {
token = Jwts.builder().setSubject(email).claim("roles", appUser.getRoles()).setIssuedAt(new Date())
.signWith(SignatureAlgorithm.HS256, "secretkey").compact();
tokenMap.put("token", token);
tokenMap.put("user", appUser);
return new ResponseEntity<Map<String, Object>>(tokenMap, HttpStatus.OK);
} else {
tokenMap.put("token", null);
return new ResponseEntity<Map<String, Object>>(tokenMap, HttpStatus.UNAUTHORIZED);
}
}
我一直在接收空令牌和401状态。
代码似乎是正确的。你知道我哪里出错了吗?你对密码使用了加密吗?我使用的是PasswordEncoder,所以你无法将输入的密码与数据库中加密的密码进行比较。试着记录你的价值观,然后看看。嗯,你是对的。谢谢你的帮助。顺致敬意,
angular.module('app')
.controller('LoginController', function ($http, AuthService, $rootScope, $scope, $location){
var vm = this;
$scope.login = function () {
// requesting the token by usename and passoword
$http({
url: 'user-login',
method: "POST",
params: {
email: $scope.email,
password: $scope.password
}
})
.then(function success(res){
$scope.password = null;
// checking if the token is available in the response
if (res.token) {
vm.message = '';
// setting the Authorization Bearer token with JWT token
$http.defaults.headers.common['Authorization'] = 'Bearer ' + res.token;
// setting the user in AuthService
AuthService.user = res.user;
$rootScope.authenticated = true;
// going to the home page
$location.path('/home');
}
else {
// if the token is not present in the response then the
// authentication was not successful. Setting the error message.
vm.message = 'Authetication Failed !';
}
},function error (error) {
vm.message = 'Authetication Failed !';
});
}
});