Spring 弹簧靴执行器安全服务无法正常工作
我尝试在调用时保护spring执行器服务/管理上下文路径,例如: 在我的yalm.properties中使用此配置Spring 弹簧靴执行器安全服务无法正常工作,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,我尝试在调用时保护spring执行器服务/管理上下文路径,例如: 在我的yalm.properties中使用此配置 管理: 港口:9091 地址:127.0.0.1 上下文路径:/manage 安全: 已启用:true 角色:管理员 但每项服务都是免费的 Spring安全配置: “@覆盖 受保护的无效配置(HttpSecurity http)引发异常{ http.authorizeRequests().antMatchers("/pizzas","/info","/addPizza"
管理:
港口:9091
地址:127.0.0.1
上下文路径:/manage
安全:
已启用:true
角色:管理员
但每项服务都是免费的
Spring安全配置:
“@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http.authorizeRequests().antMatchers("/pizzas","/info","/addPizza").hasAnyRole("USER","ADMIN").and().authorizeRequests().antMatchers("/users","/addUser").hasRole("ADMIN").and().authorizeRequests().antMatchers("/static/**","/logout","/login").permitAll();
http.formLogin().loginPage("/login").failureUrl("/login?error").permitAll();
http.logout().logoutSuccessUrl("/?logout").deleteCookies("remember-me").permitAll();
http.sessionManagement().maximumSessions(1).
expiredUrl("/?expired").maxSessionsPreventsLogin(true).and()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
}
/**
* Configure global security with Bccyptenoncder and custom userDetailService with Spring Security
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsServiceImpl).passwordEncoder(passwordEncoder());
}
/**
* Bcrypt password encoding configuration, more info at http://www.baeldung.com/spring-security-registration-password-encoding-bcrypt
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
'
Spring boot团队已经解决了这个问题。我在这里分享解决方案: 同一原产地政策 您不能在启动器安全性内使用主Spring应用程序的登录页面。原因是cookie将与应用程序的域+端口+上下文路径相关联。这是同源策略的一部分 这意味着,如果您将用户发送到localhost:9090/pizza/login并通过身份验证,那么当您访问localhost:9091/manage/JSESSIONID cookie时,它将不会提交到管理应用程序,这意味着您将不会被视为通过身份验证 为了跨域(即本例中的不同端口)进行身份验证,您需要某种单一登录(OpenID、CAS、SAML等)机制 在管理应用程序中映射登录页面 为了使用此配置,您需要在管理应用程序中设置一个登录页。要做到这一点,您只需要在请求/login时返回一个HTML表单。但是,我不确定您将如何在引导管理应用程序中执行此操作。也许@philwebb或@dsyer可以详细说明如何执行此操作t 管理应用程序的独特安全配置 或者,您可以为管理应用程序创建单独的安全配置,以允许使用基本身份验证进行身份验证。为此,您可以创建另一个类似以下内容的安全配置:
@Order(0)
@Configuration
public class ManagementSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.requestMatchers(request -> "/manage".equals(request.getContextPath()))
.and()
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
}
这将确保如果上下文根为“/manage”,则使用此安全配置。有几个要点:
@顺序(0)确保在其他安全配置之前进行配置,因为默认情况下,WebSecurityConfigureAdapter的任何子类的顺序都为100。这一点很重要,因为只使用第一个WebSecurityConfigureAdapter(类似于authorizeRequests()匹配器)。
请求匹配器正在使用lambda在contextPath上进行匹配。我原以为有更好的方法来区分Spring Boot应用程序和主应用程序,但情况似乎并非如此。也许@dsyer知道应该如何做到这一点。
注
您可以更简洁地重写配置,如下所示:
http
.authorizeRequests()
.antMatchers("/pizzas","/info","/addPizza").hasAnyRole("USER","ADMIN")
.antMatchers("/users","/addUser").hasRole("ADMIN")
.antMatchers("/static/**","/logout","/login").permitAll()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/?logout")
.deleteCookies("remember-me")
.permitAll();
您可以考虑阅读Spring安全java配置预览:关于如何配置配置的详细信息,以便更好地阅读它。
我更新了项目。它工作得很好,但是当我访问端点应用程序时,我的HTTP请求重定向到管理/登录,这显然不存在,因为我的应用程序具有上下文路径/比萨饼具有Logi。n、 哪种方法可以重定向到启动器端点中的自定义登录?