Which of the following best explains why Spring Security is important in a web application?
Think about what security means for users accessing a web app.
Spring Security helps protect applications by managing who can access what, ensuring only authorized users get in.
When you add Spring Security to a Spring Boot application without any extra configuration, what is the default behavior?
Think about what Spring Security does out of the box to protect your app.
By default, Spring Security locks down all endpoints and creates a default user with a generated password to require login.
Consider this Spring Security filter chain setup:
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated())
.formLogin(withDefaults());What happens when an unauthenticated user tries to access /public/info and /private/data?
Look at which paths are allowed without authentication.
The configuration permits all requests under /public/** without login, but requires authentication for any other request.
Which of the following code snippets correctly configures Spring Security to disable CSRF protection and allow all requests to /api/** without authentication?
Check the method to disable CSRF and the correct order of authorization rules.
Option A disables CSRF correctly and permits all requests to /api/** while requiring authentication for others.
Given this Spring Security configuration method:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated())
.formLogin();
return http.build();
}When running the app, it throws an error: java.lang.IllegalArgumentException: Role prefix 'ROLE_' is missing. What is the cause?
Think about how Spring Security expects role names internally.
Spring Security expects roles to have a 'ROLE_' prefix internally. The hasRole("ADMIN") method automatically adds this prefix, so the actual role must be stored as 'ROLE_ADMIN'. If the prefix is missing in the stored roles, this error occurs.