首页 > 解决方案 > Spring Security 不适用于 Weblogic 12.2.1.2

问题描述

我有一个使用Spring Security的 Web 应用程序。它适用于Tomcat,但不适用于Weblogic 12.2.1.2

在 Weblogic 上,当用户尝试访问受限 URL(例如 localhost:7001/website/restricted/welcome)时,不会将用户重定向到登录页面。在 Tomcat 上,用户被正确重定向到登录页面。

我读到这是Weblogic 12.1的一个错误,它似乎已在 Weblogic 12.2 中修复。但我使用的是 Weblogic 12.2.1.2并且遇到了同样的问题。

我阅读了一些解决方案,但我很难理解它们,因为我有不同的 Spring 配置。

这些是我关于 Spring Security 的课程。

这是扩展WebSecurityConfigurerAdapter的类。

        @Configuration
        @EnableWebSecurity
        public class SecurityConfig extends WebSecurityConfigurerAdapter {

            @Autowired
            public void configureGlobalSecurity(AuthenticationManagerBuilder auth) 
            throws Exception {     
                  auth.inMemoryAuthentication()
                  .passwordEncoder(passwordEncoder())
                  .withUser("username1")
                  .password(passwordEncoder()
                  .encode("password1"))
                  .roles("ADMIN");                      
            }            

            @Bean
            public PasswordEncoder passwordEncoder() {
                  BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                  return passwordEncoder;
           }     

           @Override
           protected void configure(HttpSecurity httpSecurity) throws Exception {

                  httpSecurity.formLogin()
                  .loginPage("/login")
                  .usernameParameter("userId")
                  .passwordParameter("password");

                  httpSecurity.formLogin()
                  .defaultSuccessUrl("/")                 
                  .failureHandler(new CustomAuthenticationFailureHandler())
                  .and()
                  .sessionManagement()  
                  .maximumSessions(1) 
                  .expiredUrl("/login?expired")
                  .maxSessionsPreventsLogin(true);

                  httpSecurity.logout()           
                  .logoutSuccessUrl("/login?logout");

                  httpSecurity.rememberMe()         
                  .rememberMeParameter("rememberMe") 
                  .key("rememberMeKey") 
                  .tokenValiditySeconds(1800);  

                  httpSecurity.exceptionHandling()
                  .accessDeniedPage("/login?accessDenied"); 

                  httpSecurity.authorizeRequests()              
                  .antMatchers("/").permitAll() 
                  .antMatchers("/**/add").access("hasRole('ADMIN')")  
                  .antMatchers("/**/market/**").access("hasRole('USER')");                      

                  httpSecurity.csrf() 
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
           }                 

    }

这是扩展AbstractSecurityWebApplicationInitializer的类。

public class SecurityWebApplicationInitializer 
extends AbstractSecurityWebApplicationInitializer 
implements WebApplicationInitializer {

     @Override
     protected boolean enableHttpSessionEventPublisher() {         
         return true;
     }

}

问题似乎与Spring Boot相关。当使用Spring Tool Suite时,我使用Spring Starter Project 向导(Spring boot)遇到问题。如果我不使用 Spring Boot,Spring Security 可以正常工作!

我应该如何解决这个问题?谢谢

标签: javaspringspring-securityweblogic

解决方案


推荐阅读