如何解决注销api调用,返回500

时间:2019-05-14 14:22:24

标签: java angularjs spring spring-security

我在REST中编写了一个注销api,但是在调用它时,返回500,并且url更改为/ login?logout,作为响应,它显示了index.html页面

我尝试返回登录页面或简单字符串,或者尝试了void方法,但是没有运气。解决,我们正在做角

我的api

  @CrossOrigin(maxAge=4800, allowCredentials="false")
    @RequestMapping(value="/logout", method=RequestMethod.GET)
    public String logout(HttpServletRequest req, HttpServletResponse res){
        logger.info("loging out the user and invalidating the session");
        try{
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth != null)
            new SecurityContextLogoutHandler().logout(req, res, auth);
        logger.info("----user log out sucessfully----");
        }catch(Exception e){
            logger.error("Error while logout the user: "+e.getMessage());
        }
        return "redirect:/login?logout";
        }

我尝试了return "/"return "success"

角度代码

 logOut() {
    this.logoutUser().subscribe(res=>{
      console.log("logout sucessfully")
    });
    this.router.navigate(['login']); //redirect to login
  }

错误代码:

  

严重:Servlet [Reminder365API]的Servlet.service()与   路径[/ Reminder365]引发异常   com.fasterxml.jackson.databind.JsonMappingException:没有要映射的内容   由于[[来源:   org.apache.catalina.connector.CoyoteInputStream@6b20e469;行:1   栏:0]   com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)     在   com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3829)     在   com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3774)     在   com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2899)     在   com.reminder.security.JWTLoginFilter.attemptAuthentication(JWTLoginFilter.java:44)     在   org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在   org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)

它显示 JWTLoginFilter.attemptAuthentication(JWTLoginFilter.java:44)

中的错误

代码形式JWTLoginFilter.java

    public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res)
            throws AuthenticationException, IOException, ServletException {
        logger.info("attemptAuthentication(req=" + req + ", res=" + res + ") - start - entering attemptAuthentication");
        AccountCredentials creds = new ObjectMapper().readValue(req.getInputStream(), AccountCredentials.class); //line 44

        return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(creds.getUsername(),
                creds.getPassword(), Collections.emptyList()));
    }

我希望api可以注销并重定向到登录,而不会出现任何错误,而在角度上,我们将其称为

0 个答案:

没有答案