java中怎么判断返回200_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;

}

}

调节器

@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 authenticate(String email, String password, HttpServletRequest request,

HttpServletResponse response){

if (email != null && password != null){

try {

SignedJWT token = authUserPassword.authenticate(email, password);

if (token != null){

return new ResponseEntity(String.format(defaultTokenResponse, token.serialize()),

HttpStatus.OK);

} else {

return new ResponseEntity(HttpStatus.UNAUTHORIZED);

}

} catch (BadCredentialsException badCredentials) {

return new ResponseEntity(HttpStatus.UNAUTHORIZED);

}

} else {

return new ResponseEntity(HttpStatus.UNAUTHORIZED);

}

}

}

测试类:

@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();

}

}

如果有人对其余代码感兴趣,请点击链接:repository

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值