gpt4 book ai didi

java - 如何解决多个formLogin()无法找到其他loginProcessUrl()的问题?

转载 作者:行者123 更新时间:2023-12-02 12:17:31 25 4
gpt4 key购买 nike

所以我有两个登录页面。一份用于客户,一份用于 AM。我在 WebSecurityConfig 类中配置了 2 个登录页面。当我尝试使用管理员帐户登录 AM 时,它可以工作,但是当我尝试使用用户帐户登录 Customer 时,找不到 loginProcessingUrl。

在我的WebSecurityConfig类中:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig
{

@Autowired
MyDBAuthenticationService myDBAuthenticationService;

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(myDBAuthenticationService);
}

@Configuration
@Order(1)
public static class WebConfigurationAdapter1 extends WebSecurityConfigurerAdapter
{

@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests().antMatchers("/am/**").access("hasRole('ROLE_AM')")

.and()
.exceptionHandling()
.accessDeniedPage("/403")

.and()
.formLogin()
.loginPage("/amLogin")
.loginProcessingUrl("/am/postLogin")
.defaultSuccessUrl("/amChatPage")
.failureUrl("/amLogin?error")
.and().logout().logoutUrl("/amLogout").logoutSuccessUrl("/amLogoutSuccessful")
.deleteCookies("JSESSIONID")
.and().csrf().disable();

System.out.println("1st Configurer");
}
}

@Configuration
@Order(2)
public static class WebConfigurationAdapter2 extends WebSecurityConfigurerAdapter
{

@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests().antMatchers("/customer/**").access("hasRole('ROLE_CUSTOMER')")

.and()
.exceptionHandling()
.accessDeniedPage("/403")

.and()
.formLogin()
.loginPage("/customerLogin")
.loginProcessingUrl("/customer/postLogin")
.defaultSuccessUrl("/customerChatPage")
.failureUrl("/customerLogin?error")
.and().logout().logoutUrl("/customerLogout").logoutSuccessUrl("/customerLogoutSuccessful")
.and().csrf().disable();

System.out.println("2nd Configurer");
}
}
}

这是我的 SpringWebAppInitializer 类:

@Configuration
public class SpringWebAppInitializer implements WebApplicationInitializer{

@Override
public void onStartup(ServletContext sc) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(ApplicationContextConfig.class);

ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}

}

到目前为止我所做的事情是:

以下是我以 AM 身份登录时的结果: enter image description here

结果为 netbeans。它进入 Controller 但显示“null” enter image description here

以下是我以客户身份登录时的结果: enter image description here

最佳答案

由于两个 http 配置之间没有区分模式,Spring Security 将第一个登录作为默认值,这就是为什么只有管理员登录有效的原因,因为它是声明为 Order(1) 的 Web 安全适配器的一部分。 .

为了正确分离这两个配置,有必要使用.antMatcher定义模式分离。 .

这是一个小例子,让您了解如何定义这两种配置

对于管理员(请参阅 .antMatcher 定义,它强制将 http 配置仅应用于 admin/** url。

@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.antMatcher("/admin/**").authorizeRequests().anyRequest().authenticated().anyRequest().hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/adminLogin")
.loginProcessingUrl("/admin/postLogin")
.defaultSuccessUrl("/admin/home")
.failureUrl("/adminLogin?error")
.and().logout().logoutUrl("/admin/logout").logoutSuccessUrl("/home")
.and()
.csrf().disable();

}

对于客户(请参阅 .antMatcher 定义,它强制将 http 配置仅应用于 customer/** url。

        @Override
protected void configure(HttpSecurity http) throws Exception
{
http
.antMatcher("/customer/**").authorizeRequests().anyRequest().authenticated().anyRequest().hasRole("USER")
.and()
.formLogin()
.loginPage("/customerLogin")
.loginProcessingUrl("/customer/postLogin")
.defaultSuccessUrl("/customer/home")
.failureUrl("/customerLogin?error")
.and()
.logout().logoutUrl("/customer/logout").logoutSuccessUrl("/home")
.and()
.csrf().disable();

}

这里还有其他示例:Example two login pages并访问 Spring Security 文档 Multiple Http Security

希望这些信息对您有所帮助。

根据新的配置你必须做一些改变,请查看以下配置,并与你的配置进行比较,你会发现有什么不同( antMatcherantMatchers 不同)

@Configuration
@EnableWebSecurity
public class WebSecurityConfig
{

@Autowired
MyDBAuthenticationService myDBAuthenticationService;

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(myDBAuthenticationService);
}

@Configuration
@Order(1)
public static class WebConfigurationAdapter1 extends WebSecurityConfigurerAdapter
{

@Override
protected void configure(HttpSecurity http) throws Exception
{
http
//.authorizeRequests().antMatchers("/am/**").access("hasRole('ROLE_AM')")
.antMatcher("/am/**").authorizeRequests().anyRequest().hasRole("AM")

.and()
.exceptionHandling()
.accessDeniedPage("/403")

.and()
.formLogin()
.loginPage("/amLogin")
.loginProcessingUrl("/am/postLogin")
.defaultSuccessUrl("/am/chatPage")
.failureUrl("/amLogin?error")
.and().logout().logoutUrl("/am/logout").logoutSuccessUrl("/amLogoutSuccessful")
.deleteCookies("JSESSIONID")
.and().csrf().disable();

System.out.println("1st Configurer");
}
}

@Configuration
@Order(2)
public static class WebConfigurationAdapter2 extends WebSecurityConfigurerAdapter
{

@Override
protected void configure(HttpSecurity http) throws Exception
{
http
//.authorizeRequests().antMatchers("/customer/**").access("hasRole('ROLE_CUSTOMER')")
.antMatcher("/admin/**").authorizeRequests().anyRequest().hasRole("CUSTOMER")

.and()
.exceptionHandling()
.accessDeniedPage("/403")

.and()
.formLogin()
.loginPage("/customerLogin")
.loginProcessingUrl("/customer/postLogin")
.defaultSuccessUrl("/customer/chatPage")
.failureUrl("/customerLogin?error")
.and().logout().logoutUrl("/customer/logout").logoutSuccessUrl("/customerLogoutSuccessful")
.and().csrf().disable();

System.out.println("2nd Configurer");
}
}
}

最后记住 Controller ,你应该有以下RequestMapping至少定义

@RequestMapping("/adminLogin") , @RequestMapping("/customerLogin") , @RequestMapping("/am/chatPage") , @RequestMapping("/customer/chatPage")

关于java - 如何解决多个formLogin()无法找到其他loginProcessUrl()的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46046743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com