Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 具有Spring安全性的透明身份验证_Java_Spring_Spring Security - Fatal编程技术网

Java 具有Spring安全性的透明身份验证

Java 具有Spring安全性的透明身份验证,java,spring,spring-security,Java,Spring,Spring Security,我已经用Spring4和SpringSecurity3.2实现了我的服务器 我正在使用两种可能的身份验证方案,这取决于用户客户端类型、通过html表单进行身份验证的web应用程序以及Android或iOS等移动客户端 当会话过期时,用户移动应用程序可以离开服务器处理失去身份验证的应用程序,在这种情况下,我试图通过Authenticator header param和一个自定义入口点对客户端进行身份验证,如下图所示 public class AuthEntryPoint implements Au

我已经用Spring4和SpringSecurity3.2实现了我的服务器

我正在使用两种可能的身份验证方案,这取决于用户客户端类型、通过html表单进行身份验证的web应用程序以及Android或iOS等移动客户端

当会话过期时,用户移动应用程序可以离开服务器处理失去身份验证的应用程序,在这种情况下,我试图通过Authenticator header param和一个自定义入口点对客户端进行身份验证,如下图所示

public class AuthEntryPoint implements AuthenticationEntryPoint {

    @Autowired
    private RestProvider restProvider;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authenticationException) 
            throws IOException, ServletException {

        Device device = (Device) request.getAttribute("device");

        if (device.isNormal()) { // WEB

            response.sendRedirect(response.encodeRedirectURL(request.getContextPath()));
        } else { // MOBILE

            DeviceAuth deviceAuth = new DeviceAuth(request.getHeader("Authorization"));

            UserAuthToken userAuthToken = (UserAuthToken) this.restProvider.authenticate(
                    new IncomingToken(
                            deviceAuth.getEmail(),
                            null,
                            "user",
                            deviceAuth.getNode(),
                            deviceAuth.getAuthToken())
            );

            if (userAuthToken != null) {

                SecurityContextHolder.getContext().setAuthentication(userAuthToken);
            }

            if (request.getRequestURI() != null) {

                response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + 
                        request.getRequestURI()));
            } else {

                response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Not page");
            }
        }
    }
}
设备类预先加载到来自一个自定义筛选器的请求中,该筛选器分析请求以确定设备类型

RestProvider是返回UserAuthToken的两个应用程序提供程序之一,UserAuthToken是UsernamePasswordAuthenticationToken的一个自定义实现

所有这些工作都很好,除了重定向问题,我希望实现透明的身份验证过程,即当用户离开去处理应用程序,并在其服务器会话(和身份验证)被破坏后返回使用它时,服务器识别坏凭证,获取身份验证标头以对应用程序透明地进行身份验证并继续执行请求


我能怎么办?

自己回答我的问题

我没有实现AuthenticationEntryPoint,而是用我在上一篇文章中看到的相同逻辑实现了BasicAuthenticationFilter,删除了正常的设备控件,因为普通的internet Explorer无法使用BasicAuthentication

public class BasicAuthFilter extends BasicAuthenticationFilter {

    ...

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                FilterChain filterChain) throws IOException, ServletException {

    ...
    }
}
您可以阅读更多有关基本认证的信息

public class BasicAuthFilter extends BasicAuthenticationFilter {

    ...

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                FilterChain filterChain) throws IOException, ServletException {

    ...
    }
}