当前位置: 首页 > 知识库问答 >
问题:

如何在Spring Security登录请求处理之前拦截它?

拓拔欣嘉
2023-03-14

我试图在spring security开始处理或到达spring security拦截器之前拦截登录请求。我想这样做是因为我必须对用户的输入进行一些验证。因此,我从HandlerInterceptorAdapter类创建了一个拦截器,但它不起作用。

所以我想做的是:

当任何用户尝试使用用户名和密码登录时,该请求必须首先到达LoginRequestInterceptor。对用户输入进行一些验证,如果成功,将此请求传递给spring security login processing url,或者其他一些url。

public class LoginRequestInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LogManager.getLogger(LoginRequestInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        logger.debug("Interceptor working pre");
        return super.preHandle(request, response, handler);
    }
<http auto-config="true" use-expressions="true">
    <intercept-url  pattern="/Candidate/**" access="hasRole('ROLE_USER')" />
    <!-- access denied page -->
    <access-denied-handler error-page="/403" />
    <form-login 
        login-processing-url="/login"
        login-page="/" 
        default-target-url="/Candidate"
        authentication-failure-url="/error=1" 
        username-parameter="registerationid"
        password-parameter="password" 
        />
    <logout logout-success-url="/?logout" />
    <custom-filter ref="loginRequestInterceptors" position="FIRST"/>
    <!-- enable csrf protection -->
    <csrf />
</http>

<authentication-manager id="authenticationManager">
    <authentication-provider user-service-ref="candidateDetailServices" />
</authentication-manager>

Spring dispatcher xml

    <bean id="loginRequestInterceptor" class="org.ibps.clerk.inteceptors.login.LoginRequestInterceptor"></bean>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/login"/>
            <ref bean="loginRequestInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

所以最后我想知道这是否可能?如果是,那么请分享链接或解决方案

共有1个答案

云鸿祯
2023-03-14

您应该创建一个类(例如CustomFilter),扩展UsernamePasswordAuthenticationFilter,然后在AttributeThentication中执行您的操作,然后调用Super.AttributeThencication

然后,您应该在扩展WebSecurityConfigurerAdapter的安全配置类中进行解释。

@Configuration
@EnableWebSecurity
public class SecurityConfigs extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(getCustomFilter()
        ,UsernamePasswordAuthenticationFilter.class)
            .formLogin()
            .and() // more configs ...
}


public CustomFilter getCustomFilter() throws Exception {
    CustomFilter filter= new CustomFilter ("/loginuser","POST");
    filter.setAuthenticationManager(authenticationManagerBean());
    filter.setAuthenticationFailureHandler((request, response, exception) -> {
        response.sendRedirect("/login?error");
    });
    return filter;
}

CustomFilter类:

public class CustomFilter extends UsernamePasswordAuthenticationFilter {

    public CustomFilter (String urlLogin, String method) {
        setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher(urlLogin, method));
    }


    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        // do your stuff ...

        return super.attemptAuthentication(request, response);
    }
}
 类似资料:
  • 一、拦截请求 mitmproxy的强大功能是拦截请求。拦截的请求将暂停,以便用户可以在将请求发送到服务器之前修改(或丢弃)该请求。mitmproxy的set intercept命令配置拦截。i默认情况下,该命令绑定到快捷方式。 通常不希望拦截所有请求,因为它会不断中断您的浏览。因此,mitmproxy希望将流过滤器表达式作为set intercept选择性拦截请求的第一个参数。在下面的教程中,我们

  • 配置拦截器 declarations: [ AppComponent ], HttpClientModule ], providers: [ [ { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true } ] bootstrap:

  • 在 imi 中更加推荐使用 AOP 来拦截请求。 不要忘记把 Aspect 类加入 beanScan! Demo <?php namespace ImiApp\ApiServer\Aop; use Imi\RequestContext; use Imi\Aop\Annotation\Around; use Imi\Aop\Annotation\Aspect; use Imi\Aop\Annota

  • 本文向大家介绍SpringCloud通用请求字段拦截处理方法,包括了SpringCloud通用请求字段拦截处理方法的使用技巧和注意事项,需要的朋友参考一下 背景 以SpringCloud构建的微服务系统为例,使用前后端分离的架构,每个系统都会提供一些通用的请求参数,例如移动端的系统版本信息、IMEI信息,Web端的IP信息,浏览器版本信息等,这些参数可能放在header里,也可以放在参数里,如果这

  • 我正在研究SSO的一些新用法。基本上,我正在尝试找到如何拦截SAML请求的方法,该请求通过某种IdP代理或第三方服务从服务提供商发送到身份提供商,该服务将保存SAML请求并为用户提供一些附加功能。所需的过程可能如下所示: 用户从SP调用SAML请求-例如单击登录按钮 用户被重定向到第3方服务,例如,小型调查(这是理论示例) 提交调查后,用户被重定向到IdP并应继续登录 我对SimpleSAMLph

  • 所以我来自AngularJS的背景,在学习Angular 5时,我仍然不能把我的头放在观察物上。 我正在尝试为我的身份验证服务编写一个HTTP拦截器。但是,当我试图从服务(或此时可观察到的任何服务)获取值时,我不太确定如何正确返回下一个获取数据后从可观察对象获取方法(使用subscribe?) 这是我目前的代码(不工作): 请注意,我没有对数据做任何事情,只是试图返回可观测数据 提前谢谢你的帮助