I have a small test Spring Boot application that includes a controller that contains a simple service. The service is just a test service that causes the browser to display some HTML, as shown below:
@RestController public class TestController { @GetMapping("/testserv") public String getUserInformation() { return("<p>User authenticated!</p>"); } }
I also have a configuration class designed to require a user to authenticate in order to access the service. The intent is to make all other services in the application accessible without logging in, while accessing /testserv requires login:
@Configuration @EnableWebSecurity public class TestSecure extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity httpSecure) throws Exception { httpSecure.authorizeRequests() .antMatchers("/testserv").authenticated() .anyRequest().permitAll(); } }
If I run my application without TestSecure class, I can access the /testserv service without problems. However, when I put in the TestSecure class, I get 404 Not Found failures trying to access the service.
I did some more testing. I found that what seems to cause the Not Found failures is the line:
.antMatchers("/testserv").authenticated()
If I comment out that line I can access /testserv. With that line in there, the application suddenly cannot find the /testserv service and generates the 404 Not Found failures.
I believe I have done everything required in order for this to work, but perhaps I am missing something? Can anyone tell me why using antMatchers() would cause a service to be not found? How can I get proper access to this service?
Advertisement
Answer
The issue is that you are missing @RequestMapping
for the controller. I think just adding that annotation should get your code working.
Updated based on OP comment
You should also use .formLogin()
in order to show the login form