【问题标题】:spring security oauth2 manipulate request url before redirectspring security oauth2在重定向之前操纵请求url
【发布时间】:2017-06-06 16:05:37
【问题描述】:

我有一个使用 spring security OAuth2 保护的 Vaadin 应用程序。这工作正常,除了偶尔使用 PUSH 或 HEARTBEAT 端点首先请求并因此触发身份验证过程并且用户最终进入错误页面(用户不应直接访问这些端点)。

一个简单但不安全的修复方法是在这些端点上使用permitAll()。然而,由于这构成了威胁,我需要关闭这个漏洞。

为此,我想解析并可能编辑请求 url,然后在成功验证时重定向到它。我该怎么做呢?

我想我需要在链中的某处添加一个过滤器来拦截请求并对其进行编辑。但我不确定在哪里。

这是我的客户:

@Configuration
@EnableOAuth2Sso
public class OAuthConfig extends WebSecurityConfigurerAdapter
{

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/login**").permitAll()
                .antMatchers("/vaadinServlet/PUSH/**").permitAll()          //todo fix this hole
                .antMatchers("/vaadinServlet/HEARTBEAT/**").permitAll()      //todo fix this hole
                .anyRequest().authenticated()
                .and()
                .logout()
                .logoutSuccessUrl("/")
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));

    }

    @Override
    public void configure(WebSecurity web) throws Exception
    {
        web.ignoring().antMatchers("/css/*").antMatchers("/VAADIN/**"); // Static resources are ignored
    }

}

还有服务器:

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter
{
//jwt token stuff & my own client/auth providers. Should not be important.
...
}

服务器登录表单:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{

    @Autowired
    private RestAuthenticationProvider authenticationProvider;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.authenticationProvider(authenticationProvider);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/forgetPassword*").permitAll()
                .antMatchers(HttpMethod.POST,"/user/resetPassword*").permitAll()
                .antMatchers(HttpMethod.GET,"/user/changePassword*").permitAll()
                .antMatchers("/user/updatePassword*", "/user/savePassword*", "/updatePassword*")
                .hasAnyAuthority("CHANGE_PASSWORD_PRIVILEGE","ROLE_USER")
                .anyRequest().authenticated()
                .and()
                    .formLogin()
                    .loginPage("/login")
                    .permitAll()
                .and()
                    .csrf().csrfTokenRepository(csrfTokenRepository());
    }

    private CsrfTokenRepository csrfTokenRepository()
    {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }

}

【问题讨论】:

    标签: spring spring-security spring-security-oauth2


    【解决方案1】:

    只需在您的项目中添加一些实现

    1:创建身份验证失败处理程序

    @Component
    public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    
    
        @Override
        public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
            System.out.print("here failure");
    
    
    
            String s=request.getParameter("username");
            setDefaultFailureUrl("/login?error&username="+s);
            super.onAuthenticationFailure(request,response,exception);
        }
    
    }
    

    2:身份验证成功处理程序

    @Component
    public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request , HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            /* custom Block 
    Do any thing here
      */
    
            setDefaultTargetUrl("/home/");
            super.onAuthenticationSuccess(request,response,authentication);
        }
    }
    

    3:访问请求入口点

    @Component
    public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
        @Override
        public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
            System.out.print("Unauthorized Access");
    
            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }
    }
    

    根据您的要求实施组件。

    【讨论】:

    • 感谢您的回答,但是您能澄清一下这些是做什么用的吗?我应该将这些添加到服务器还是客户端?如果不使用用户服务,为什么要自动装配它?
    • 是的,我看到你删除了用户服务。但我仍然不知道如何使用这个答案。我认为 successhandler 将是我真正需要的唯一部分,但我也不知道在哪里添加它。
    • 按照我的示例在组件扫描目录下创建这些类,并在导航的时间和地点调试您的项目。
    • 您能否提供有关“根据您的要求实施组件”的提示。因为即使它们被正确扫描,它们也不会被注入任何地方,因此不会被使用......因为我使用了@EnableOAuth2Sso 注释,所以我没有明显的地方可以将它们附加到。
    猜你喜欢
    • 2020-01-23
    • 2021-08-08
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 2019-05-24
    • 2010-12-18
    • 2016-07-19
    相关资源
    最近更新 更多