How to link conditional methods in Java 8
•
Java
I have a spring security configuration method I want a specific method link ant matchers ("/ * * / * *") Permitall() only if the conditions match Such things {dev = = true?. antmatchers ("/ * * / * *"). Permitall(): () – > {}} Of course, this is not an effective grammar. What is the most important thing to do Look for the menimum code
@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(); }
Solution
The only way is to assign intermediate objects to variables
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();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码