How do I disable resolving login parameters passed as url parameters / from the url

后端 未结 4 1078
攒了一身酷
攒了一身酷 2020-12-03 09:28

The application logs all requested urls. This means, that it\'s critical not to authenticate using url parameters, because it would cause the situation in which

4条回答
  •  再見小時候
    2020-12-03 10:25

    I would like to suggest an alternative which is based on spring-security rater then a workaround as suggested by chimmi.

    This answer provide a solution to the issue suggested by xenteros on bres26 answer as well

    Override the exiting UsernamePasswordAuthenticationFilter implementation

    public class ImprovedUsernamePasswordAuthenticationFilter 
                                        extends UsernamePasswordAuthenticationFilter {
    
        @Override
        protected String obtainUsername(HttpServletRequest request) {
            final String usernameParameter = getUsernameParameter();
            validateQueryParameter(request, usernameParameter);
            return super.obtainUsername(request);
        }
    
        @Override
        protected String obtainPassword(HttpServletRequest request) {
            final String passwordParameter = getPasswordParameter();
            validateQueryParameter(request, passwordParameter);
            return super.obtainPassword(request);
        }
    
        private void validateQueryParameter(HttpServletRequest request, String parameter) {
            final String queryString = request.getQueryString();
            if (!StringUtils.isEmpty(queryString)) {
                if (queryString.contains(parameter))
                    throw new AuthenticationServiceException("Query parameters for login are a prohibit, use message body only!");
    
            }
        }
    
     }
    

    You need to replace your own implementation with the existing one (see doc here)

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/", "/home","/login").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .logout()
                    .permitAll()
                    .and()
                 //Replace FORM_LOGIN_FILTER with your own custom implementation
                 .addFilterAt(improvedUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                   .exceptionHandling()
                   .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
                   .and()
                //disable csrf to allow easy testing
                 .csrf().disable();
        }
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth
                    .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER");
        }
    
        public UsernamePasswordAuthenticationFilter improvedUsernamePasswordAuthenticationFilter() throws Exception {
            UsernamePasswordAuthenticationFilter authFilter = new ImprovedUsernamePasswordAuthenticationFilter();
            authFilter.setRequiresAuthenticationRequestMatcher(
                    new AntPathRequestMatcher("/login", "POST")
             );
            authFilter
            .setAuthenticationManager(authenticationManager());
            authFilter
           .setAuthenticationSuccessHandler(
               new SavedRequestAwareAuthenticationSuccessHandler()
            );
           authFilter
           .setAuthenticationFailureHandler(
             new SimpleUrlAuthenticationFailureHandler("/login?error")
           );
            return authFilter;
        }
    }
    

    Advantages: it’s based on spring security and flexible to changes.
    Disadvantage: Unfortunately I found Spring Java Config very hard to set and to read

    EDIT: I accepted chimmi comment and overridden obtainUsername and obtainPassword
    You can find the source code in github.

提交回复
热议问题