Spring boot Spring启动安全性获取错误凭据错误

Spring boot Spring启动安全性获取错误凭据错误,spring-boot,spring-security,jwt,Spring Boot,Spring Security,Jwt,我是Spring Boot Security的新手,尝试实现基于JWT的身份验证和授权。我已经完成了spring security所需的所有设置配置。即使有正确的凭证,从SpringSecurity我也会得到错误的凭证。 我已经硬编码了spring安全性需要提供的用户详细信息(没有数据库调用) 我的代码如下所示 SpringConfig.java package com.wocs.rest.controller; //导入com.example.polls.security.CustomUser

我是Spring Boot Security的新手,尝试实现基于JWT的身份验证和授权。我已经完成了spring security所需的所有设置配置。即使有正确的凭证,从SpringSecurity我也会得到错误的凭证。 我已经硬编码了spring安全性需要提供的用户详细信息(没有数据库调用)

我的代码如下所示

SpringConfig.java

package com.wocs.rest.controller;
//导入com.example.polls.security.CustomUserDetailsService;
//导入com.example.polls.security.JwtAuthenticationEntryPoint;
//导入com.example.polls.security.JwtAuthenticationFilter;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.http.HttpMethod;
导入org.springframework.security.authentication.AuthenticationManager;
导入org.springframework.security.config.BeanIds;
导入org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
导入org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
导入org.springframework.security.config.annotation.web.builders.HttpSecurity;
导入org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
导入org.springframework.security.config.annotation.web.configuration.websecurityConfigureAdapter;
导入org.springframework.security.config.http.SessionCreationPolicy;
导入org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
导入org.springframework.security.crypto.password.PasswordEncoder;
导入org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
导入com.wocs.rest.security.CustomUserDetails服务;
导入com.wocs.rest.security.JwtAuthenticationEntryPoint;
导入com.wocs.rest.security.JwtAuthenticationFilter;
/**
*由rajeevkumarsingh于2017年8月1日创建。
*/
@配置
@启用Web安全性
@启用全局方法安全性(
securedEnabled=true,
jsr250Enabled=true,
preprestenabled=true
)
公共类SecurityConfig扩展了WebSecurity配置适配器{
@自动连线
CustomUserDetails服务CustomUserDetails服务;
@自动连线
私有JwtAuthenticationEntryPoint未经授权的处理程序;
@豆子
公共JwtAuthenticationFilter JwtAuthenticationFilter(){
返回新的JwtAuthenticationFilter();
}
@凌驾
public void configure(AuthenticationManagerBuilder AuthenticationManagerBuilder)引发异常{
authenticationManagerBuilder
.userDetailsService(customUserDetailsService)
.passwordEncoder(passwordEncoder());
}
@Bean(BeanIds.AUTHENTICATION\u管理器)
@凌驾
公共AuthenticationManager authenticationManagerBean()引发异常{
返回super.authenticationManagerBean();
}
@豆子
公共密码编码器PasswordEncoder(){
返回新的BCryptPasswordEncoder();
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.cors()
.及()
.csrf()
.disable()
.例外处理()
.authenticationEntryPoint(未经授权的处理程序)
.及()
.会议管理()
.sessionCreationPolicy(sessionCreationPolicy.STATELESS)
.及()
.授权请求()
.antMatchers(“/”,
“/favicon.ico”,
“/***.png”,
“/***.gif”,
“/***.svg”,
“/***.jpg”,
“/***.html”,
“/***.css”,
“/***.js”)
.permitAll()
.antMatchers(“/auth/login”)
.permitAll()
.anyRequest()
.authenticated();
//添加我们的自定义JWT安全过滤器
http.addFilterBefore(jwtAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class);
}
}
CustomUserDetailsService.java

package com.wocs.rest.security;
导入com.wocs.services.user.iface.UserServiceIface;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.security.core.userdetails.userdetails;
导入org.springframework.security.core.userdetails.userdetails服务;
导入org.springframework.security.core.userdetails.UsernameNotFoundException;
导入org.springframework.stereotype.Service;
导入org.springframework.transaction.annotation.Transactional;
/**
*由rajeevkumarsingh于2017年8月2日创建。
*/
@服务
公共类CustomUserDetailsService实现UserDetailsService{
@自动连线
私有用户服务,即用户服务;
public void setUserService(UserServiceIface userService){
this.userService=userService;
}
@凌驾
@交易的
public UserDetails loadUserByUsername(字符串loginIDorPhoneOrEmail)
抛出UsernameNotFoundException{
//允许用户使用loginID、电子邮件或电话登录
//用户=新用户();
//UserPassword UserPassword=新的UserPassword();
列表权限=新建ArrayList();
权限。添加(“P1”);
权限。添加(“P2”);
权限。添加(“P3”);
UserDetails UserDetails=UserPrincipal.create(115,loginIDorPhoneOrEmail,“222”,true,permissions);
返回用户详细信息;
}
}
JWTAuthenticationEntryPoint.java

package com.wocs.rest.security;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.security.core.AuthenticationEx
{
 "timestamp":"2018-08-08T08:42:14.575+0000",
 "status":401,
 "error":"Unauthorized",
 "message":"Sorry, You're not authorized to access this resource.",
 "path":"/auth/login"
}