Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Autowired批注在AuthenticationSuccessHandler中返回null_Java_Spring Mvc_Spring Security - Fatal编程技术网

Java Autowired批注在AuthenticationSuccessHandler中返回null

Java Autowired批注在AuthenticationSuccessHandler中返回null,java,spring-mvc,spring-security,Java,Spring Mvc,Spring Security,在我的Spring安全应用程序中,我尝试在成功登录后返回cookie“记住令牌”。MyAuthenticateSccessHandlerclass自动连接RememberService类以从数据库获取“令牌”值。但autowired referenceRememberService返回空值。我确实提到了类的@Component注释,但它没有改变结果 FormAuthenticationSuccessHandler: package com.fastcheck.timesheet.common.s

在我的Spring安全应用程序中,我尝试在成功登录后返回cookie“记住令牌”。MyAuthenticateSccessHandlerclass自动连接RememberService类以从数据库获取“令牌”值。但autowired referenceRememberService返回空值。我确实提到了类的@Component注释,但它没有改变结果

FormAuthenticationSuccessHandler

package com.fastcheck.timesheet.common.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.fastcheck.timesheet.common.services.RememberMeService;


@Component
public class FormAuthenticationSuccessHandler implements AuthenticationSuccessHandler
{
    @Autowired
    public RememberMeService rememberMeService;

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException
{

    String username;
    Object principal = authentication.getPrincipal();
    if (principal instanceof UserDetails) 
        {
            username = ((UserDetails)principal).getUsername();
        } 
        else 
        {
            username = principal.toString();

        }

    System.out.println("rememberMeService :"+rememberMeService);
    if(rememberMeService != null)
        {
            Cookie cookie=new Cookie("remember_token",rememberMeService.getRememberMeToken(username));
            cookie.setMaxAge(200);
            response.addCookie(cookie);
        }


    response.setStatus(200);
    response.sendRedirect("home");


}

}

我从您的代码中了解到的是,您正在尝试实现创建SpringSecurity的目的是为您实现开箱即用


如果您正确地实现了spring安全性,我不明白为什么在您尝试执行成功的身份验证后,remember me令牌不会自动存储在用户浏览器上。

我从您的代码中了解到,您正在尝试实现创建spring安全性的目的


如果您正确地实现了spring安全性,我不明白为什么在成功验证后,在用户浏览器上不会像您尝试的那样自动存储“记住我”令牌。

记住令牌会自动发送到浏览器。您不需要单独发送。是否可以提供浏览器返回的cookie值?否则请使用下面的脚本进行解码

String cookieAsPlainText = new String(Base64.decode(cookies[i].getValue());

cookieAsPlainText作为普通值应为series:token格式。请告诉我这是否有助于记住自动发送到浏览器的令牌。您不需要单独发送。是否可以提供浏览器返回的cookie值?否则请使用下面的脚本进行解码

String cookieAsPlainText = new String(Base64.decode(cookies[i].getValue());

cookieAsPlainText作为普通值应为series:token格式。请让我知道这是否有帮助

它正在存储一些垃圾价值。不是实际的令牌值,请稍候。希望您不希望spring security在浏览器上保存的信息与持久表中存储的信息相同?如果这是您的期望,那么您误解了spring security记住我的概念。您称之为垃圾邮件的重要信息是用户名、随机生成的序列标识符和随机生成的令牌值的散列组合。Spring security将对其进行解码,并在MemberMe身份验证期间与持久表中存储的内容进行比较。请阅读此链接以更好地理解整个概念。你说得对,佩里。我需要解码记忆令牌。你能把这个作为答案贴出来吗?这样我就可以接受了。你已经接受了另一个答案。在接受答案之前,您可能没有检查姓名。嗯,如果你觉得它也有帮助的话也没关系。祝你一切顺利。继续编码。它正在存储一些垃圾值。不是实际的令牌值,请稍候。希望您不希望spring security在浏览器上保存的信息与持久表中存储的信息相同?如果这是您的期望,那么您误解了spring security记住我的概念。您称之为垃圾邮件的重要信息是用户名、随机生成的序列标识符和随机生成的令牌值的散列组合。Spring security将对其进行解码,并在MemberMe身份验证期间与持久表中存储的内容进行比较。请阅读此链接以更好地理解整个概念。你说得对,佩里。我需要解码记忆令牌。你能把这个作为答案贴出来吗?这样我就可以接受了。你已经接受了另一个答案。在接受答案之前,您可能没有检查姓名。嗯,如果你觉得它也有帮助的话也没关系。祝你一切顺利。继续编码。