如何在 Java 8 中进行条件方法链接
How to do Conditional Method chaining in Java 8
我有一个 spring 安全配置方法。我希望仅当条件匹配时才将特定方法链接 antMatchers("/**/**").permitAll()
。像这样 {dev == true ? .antMatchers("/**/**").permitAll(): ()->{}}
。当然,这不是有效的语法,最简洁 的做法是什么。正在寻找最低编码。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.cors().disable()
.authorizeRequests()
{dev == true ? .antMatchers("/**/**").permitAll(): ()->{}} //dev only. NEVER enable on prod
.antMatchers("/", "/signup", "/static/**", "/api/sigin", "/api/signup", "**/favicon.ico").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/api/signin")
.successHandler(authSuccessHandler())
.failureHandler(authFailureHandler())
.permitAll()
.and()
.logout()
.permitAll();
}
唯一的方法是将中间对象赋给一个变量。
WhateverAuthorizeRequestsReturns partial = http
.csrf().disable()
.cors().disable()
.authorizeRequests();
if (dev) // note: you don't need 'dev == true' like you had
{
partial.someOptionalThing();
// if the type is immutable then you need to reassign e.g.:
// partial = partial.someOptionalThing()
}
partial.something()
.somethingElse()
.andTheRest();
如果你只想允许访问基于布尔值的特定路径,你可以试试这个:
http
.csrf().disable()
.cors().disable()
.authorizeRequests()
.antMatchers(dev ? "/**/**":"invalid-path").permitAll()
.antMatchers("/", "/signup", "/static/**", "/api/sigin", "/api/signup", "**/favicon.ico").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/api/signin")
.successHandler(authSuccessHandler())
.failureHandler(authFailureHandler())
.permitAll()
.and()
.logout()
.permitAll();
我有一个 spring 安全配置方法。我希望仅当条件匹配时才将特定方法链接 antMatchers("/**/**").permitAll()
。像这样 {dev == true ? .antMatchers("/**/**").permitAll(): ()->{}}
。当然,这不是有效的语法,最简洁 的做法是什么。正在寻找最低编码。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.cors().disable()
.authorizeRequests()
{dev == true ? .antMatchers("/**/**").permitAll(): ()->{}} //dev only. NEVER enable on prod
.antMatchers("/", "/signup", "/static/**", "/api/sigin", "/api/signup", "**/favicon.ico").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/api/signin")
.successHandler(authSuccessHandler())
.failureHandler(authFailureHandler())
.permitAll()
.and()
.logout()
.permitAll();
}
唯一的方法是将中间对象赋给一个变量。
WhateverAuthorizeRequestsReturns partial = http
.csrf().disable()
.cors().disable()
.authorizeRequests();
if (dev) // note: you don't need 'dev == true' like you had
{
partial.someOptionalThing();
// if the type is immutable then you need to reassign e.g.:
// partial = partial.someOptionalThing()
}
partial.something()
.somethingElse()
.andTheRest();
如果你只想允许访问基于布尔值的特定路径,你可以试试这个:
http
.csrf().disable()
.cors().disable()
.authorizeRequests()
.antMatchers(dev ? "/**/**":"invalid-path").permitAll()
.antMatchers("/", "/signup", "/static/**", "/api/sigin", "/api/signup", "**/favicon.ico").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/api/signin")
.successHandler(authSuccessHandler())
.failureHandler(authFailureHandler())
.permitAll()
.and()
.logout()
.permitAll();