I have an application in Spring Boot released on Cloud Run with IAP authentication. If I text locally I get this error:
java.lang.NullPointerException: Cannot invoke “org.springframework.security.core.Authentication.getName()” because “authentication” is null
at com.wind.cbrweb.web.HomeController.handleRequest(HomeController.java:56) ~[classes/:0.0.1-SNAPSHOT]
I’m using this library:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-security-iap</artifactId>
</dependency>
my Security Context class:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
protected final Log logger = LogFactory.getLog(getClass());
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers("/**").permitAll().anyRequest().authenticated())
.oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()));
http.headers((headers) -> headers
.frameOptions((frameOptions) -> frameOptions.disable()));
return http.build();
}
}
my application properties file:
spring.cloud.gcp.security.iap.registry=https://www.gstatic.com/iap/verify/public_key-jwk
spring.cloud.gcp.security.iap.algorithm=ES256
spring.cloud.gcp.security.iap.header=x-goog-iap-jwt-assertion
spring.cloud.gcp.security.iap.issuer=https://cloud.google.com/iap
spring.cloud.gcp.security.iap.audience=/projects/PROJECT_ID/global/backendServices/SERVICE_ID
this is the controller of the first page after IAP authentication:
@Controller
public class HomeController {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private UserManager userManager;
@Autowired
private Firestore firestore;
@GetMapping("/")
public String redirectToHome(Authentication authentication) {
return "redirect:/home.htm";
}
@GetMapping("/home.htm")
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res, Authentication authentication) throws Exception {
logger.info("Returning home view");
String currentUserName = authentication.getName();
//OTHER CODE
}
}
How can I create a user to simulate Google IAP authentication? I expect that when I run the Spring /home application I have Authentication filled with the Principal information (group to which I belong, name, user_id, etc.). Then simulate the authentication as on IAP
user25447525 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.