【问题标题】:How to exclude certain files from authentication checks when using HttpAuthenticationMechanism使用 HttpAuthenticationMechanism 时如何从身份验证检查中排除某些文件
【发布时间】:2022-12-16 10:25:10
【问题描述】:

当使用我们自己的 HttpAuthenticationMechanism 实现时,如何防止针对某些文件类型触发身份验证检查?

例如。我们不希望我们的 Authentication bean 被静态资源请求触发,例如 .js / .css 文件

使用 Wildfly 26 (Java EE8)

更新:

已尝试在没有 <auth-constraint> 定义的情况下在 web.xml 中指定静态文件,这是规范所说的应该这样做的方式,但我仍然发现 validateRequest 正在为这些文件开火

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Static Content</web-resource-name>      
        <url-pattern>/jsJawrPath/*</url-pattern>
        <url-pattern>/cssJawrPath/*</url-pattern>
        <url-pattern>/javax.faces.resource/*</url-pattern>
        <url-pattern>/images/*</url-pattern>
        <url-pattern>/resources/*</url-pattern>
    </web-resource-collection>
</security-constraint>

我们的认证bean

@RequestScoped
@AutoApplySession
public class CustomAuthentication implements Serializable, HttpAuthenticationMechanism {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Inject private Logger log;

/**
 * Note: this method is called for all requests (including public) to determine if authentication is required
 */
@Override
public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response,
        HttpMessageContext httpMessageContext) throws AuthenticationException {
    
    log.debug("Validating request {}",request.getRequestURI());

    //Authentication logic...

}

}

然后在日志中...

[10:44:30.476] DEBUG (com.myapp.security.CustomAuthentication)  Validating request /jsJawrPath/jawr_generator.js

【问题讨论】:

    标签: jakarta-ee jakarta-ee-security-api


    【解决方案1】:

    它确实在各种请求上被调用。仅针对凭据也以一种或其他替代方式(例如,通过 GET 参数或 cookie 的一些 uuid 令牌)在它们上可用的情况,以便也可以在它们上执行一些自动登录机制。这是从web.xml 开始不可配置的,所以HttpAuthenticationMechanism 知道得再清楚不过了。

    如果您仅通过SecurityContext#authenticate() 直接验证(输入),那么您只需预先检查HttpMessageContext#isAuthenticationRequest() 是否返回true

    @Override
    public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response, HttpMessageContext context) throws AuthenticationException {
        if (context.isAuthenticationRequest()) {
            log.debug("Validating request {}",request.getRequestURI());
    
            // Authentication logic...
    
            return authenticationStatus;
        }
        else {
            return context.doNothing();
        }
    }
    

    否则返回HttpMessageContext#doNothing(),如图所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多