Spring Security没有用于登录页面的控制器

时间:2018-03-22 20:30:28

标签: spring spring-security

Spring Security的新功能。我正在查看此链接' https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html#grant-access-to-remaining-resources'并且真的难以理解配置登录视图控制器`。

当我创建一个典型的表单时,我通常会创建一个html页面,在单击时,调用我的自定义@controller中的方法,该方法将发送到我的逻辑等。

然而,在他们的例子中,他们声明不需要任何控制器,因为一切都是默认的'。有人可以确切地解释他们的登录表单如何连接'他们的身份验证对象?尽管没有控制器方法,看起来不知何故凭证可以神奇地传递给Authentication对象。

谢谢!

2 个答案:

答案 0 :(得分:1)

没有控制器。当您使用formLogin()方法时,会在UsernamePasswordAuthenticationFilter中注册security filter chain并执行身份验证作业。您可以查看源代码here

public Authentication attemptAuthentication(HttpServletRequest request,
            HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

        String username = obtainUsername(request);
        String password = obtainPassword(request);

        if (username == null) {
            username = "";
        }

        if (password == null) {
            password = "";
        }

        username = username.trim();

        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
                username, password);

        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);

        return this.getAuthenticationManager().authenticate(authRequest);
}

答案 1 :(得分:0)

再看看https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html#configuring-a-login-view-controller。在实际可以看到的代码片段中,注册了具有请求映射/login的内部控制器。这就是为什么你不必自己实现它。视图,内部控制器和后台身份验证管理器之间的所有身份验证传输都是完全透明的。

相关问题