大佬教程收集整理的这篇文章主要介绍了java – Spring返回401而不是200状态,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了一个应用程序作为学习Spring的一部分,但是当我测试身份验证时,我收到401状态而不是200.我正在寻找错误的原因,在我看来,行身份验证身份验证= authenticationManager.authenticate(new UsernamepasswordAuthenticationToken(电子邮件,密码));返回null.但是,我不知道如何解决这个问题.
@Component
public class AuthenticationserviceUsernamepassword {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationserviceUsernamepassword.class);
@Autowired
@Qualifier("customAuthenticationManager")
private AuthenticationManager authenticationManager;
@Autowired
private TokenManager tokenManager;
public SignedJWT authenticate(final String email,final String password){
try {
Authentication authentication = authenticationManager
.authenticate(new UsernamepasswordAuthenticationToken(email,password));
SecurityContextHolder.getContext()
.setAuthentication(authentication);
if (authentication.getPrincipal() != null) {
return tokenManager.createNewToken((PrincipalUser) authentication.getPrincipal());
}
} catch (AuthenticationException authException) {
LOGGER.debug("Authentication Failed for user:\"" + email + ".\" Reason " + authException.getClass());
}
return null;
}
}
@H_673_6@
调节器
@Controller
public class AuthController {
@Value("${jwt.result}")
private String defaultTokenResponse;
@Autowired
private AuthenticationserviceUsernamepassword authUserpassword;
@requestMapping(value = "/authentication",method = requestMethod.POST,produces = MediaType.APPLICATION_JSON_value)
public ResponseEntity @H_673_6@
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class ConnectControllerTest {
protected mockmvc mockmvc;
@Autowired
private WebApplicationContext context;
@Autowired
private Filter springSecurityFilterChain;
@Before
public void setup() {
mockmvc = mockmvcBuilders.webAppContextSetup(context)
.addFilters(springSecurityFilterChain)
.defaultrequest(get("/"))
.build();
}
@Test
public void shouldTestAuthentication() throws Exception {
String result = mockmvc.perform(post("/authentication")
.param("email","user@test.pl").param("password","password"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.token").exists())
.andReturn().getResponse().getContentAsString();
}
}
@H_673_6@
如果有人对其余代码感兴趣,请点击链接:repository
问题在这里
public SignedJWT authenticate(final String email,final String password){
try {
System.out.println("test => "+email+" : "+password);
Authentication authentication = authenticationManager
.authenticate(new UsernamepasswordAuthenticationToken(email,password));
SecurityContextHolder.getContext().setAuthentication(authentication);
if (authentication.getPrincipal() != null) {
return tokenManager.createNewToken((PrincipalUser) authentication.getPrincipal());
}
} catch (AuthenticationException authException) {
authException.printStackTrace();
LOGGER.debug("Authentication Failed for user:\"" + email + ".\" Reason " + authException.getClass());
}
System.out.println("return nulll");
return null;
}
@H_673_6@
如果运行测试用例,则会抛出以下错误
org.springframework.security.authentication.badCredentialsException: Bad credentials
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationchecks(DaoAuthenticationProvider.java:98)
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166)
at org.springframework.security.authentication.ProvideRMANager.authenticate(ProvideRMANager.java:174)
at org.springframework.security.authentication.ProvideRMANager.authenticate(ProvideRMANager.java:199)
at org.springframework.security.config.Annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:504)
at com.github.springjwt.security.jwt.service.AuthenticationserviceUsernamepassword.authenticate(AuthenticationserviceUsernamepassword.java:30)
at com.github.springjwt.web.api.controller.AuthController.authenticate(AuthController.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImp
@H_673_6@
这意味着您的测试用例的用户名和密码与UserRepository类用户详细信息不匹配
在您的UserRepository类中
您需要设置正确的哈希密码及其设置为null的盐值.
以上是大佬教程为你收集整理的java – Spring返回401而不是200状态全部内容,希望文章能够帮你解决java – Spring返回401而不是200状态所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。