AuthenticationCredentialsNotFoundException: Authentication object not found in security context

I get the following error when trying to send a JSON payload from the Chrome browser POSTMAN plugin for the controller displayed as a REST URL - http: // localhost: 8080 / services / acc / create

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/services] threw exception [Request processing failed; nested exception is org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext] with root cause org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:339) at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:198) at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:60) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) at com.webadvisors.controller.HotelRestController$$EnhancerBySpringCGLIB$$e9f80d9.createHotel(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 

I chose BasicAuth as the authorization type and entered the username and password when sending the JSON payload to the REST URL in POSTMAN.

1) Controller class

 @RestController public class AccountRestController { @Autowired private AccountService accountService; @PreAuthorize("hasAnyRole('ADMINISTRATOR')") @RequestMapping(value= "/acc/create", method=RequestMethod.POST) public HotelDTO createHotel(@RequestBody AccountDTO accDTO) throws Exception{ return accountService.create(accDTO); } } 

2) Safety class

 @Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) @EnableWebSecurity @ComponentScan(basePackages = "com.freelance", scopedProxy = ScopedProxyMode.INTERFACES) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired @Qualifier("authenticationService") private UserDetailsService userDetailsService; @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); auth.authenticationProvider(authenticationProvider()); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/user/**").permitAll() .anyRequest().fullyAuthenticated(); http.httpBasic(); http.csrf().disable(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public DaoAuthenticationProvider authenticationProvider() { DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(); authenticationProvider.setUserDetailsService(userDetailsService); authenticationProvider.setPasswordEncoder(passwordEncoder()); return authenticationProvider; } } 

3) Spring Security Dependencies

 <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>3.2.3.RELEASE</version> </dependency> 

I am sending authentication credentials to POSTMAN. But why am I getting the exception above.

+5
source share
1 answer

You created springSecurityFilterChain with WebSecurityConfigurerAdapter , see Spring Security Link :

The first step is to create our Spring Java security configuration. The configuration creates a servlet filter, known as springSecurityFilterChain , which is responsible for all security (protects application URLs, checks username and passwords, redirects to the registration form, etc.) in your application.

but you didn’t use it (this is not in your stack trace).

You need to register springSecurityFilterChain . If you have a Servlet 3.0+ environment, see Spring Security Help :

AbstractSecurityWebApplicationInitializer with Spring MVC

If we used Spring elsewhere in our application, we probably already had a WebApplicationInitializer that loads our Spring configuration. If we use the previous configuration, we will get an error. Instead, we must register Spring Security with an existing ApplicationContext . For example, if we used Spring MVC, our SecurityWebApplicationInitializer would look something like this:

 import org.springframework.security.web.context.*; public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { } 

This will simply register the springSecurityFilterChain filter for each URL of your application. After that, we would ensure that WebSecurityConfig loaded into our existing ApplicationInitializer. For example, if we used Spring MVC, it would be added to getRootConfigClasses()

 public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { WebSecurityConfig.class }; } // ... other overrides ... } 
+4
source

Source: https://habr.com/ru/post/1262965/


All Articles