Implement custom Authentication, AuthenticationFilter and AuthenticationProvider in Spring Security - java

The objective is to get a session token from the header in my custom authentication filter. Build my custom authentication object where I can pass the session token as a name. This custom authentication object will be passed to my custom authentication provider class, where I want to build an authentication object if the session token is valid.
Custom Authentication object
#Data
#NoArgsConstructor
#AllArgsConstructor
public class AuthenticationObject implements Authentication{
private static final long serialVersionUID = 1L;
private String name;
private Collection<? extends GrantedAuthority> authorities;
private Object credentials;
private Object details;
private Object principal;
private boolean authenticated;
}
Custom Filter to get token from the header and build Authentication object for passing it to AuthenticationProvider
#Component
#Slf4j
public class SessionTokenFilter extends OncePerRequestFilter {
#Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
log.info("Inside session filter");
String sessionToken = request.getHeader("session-token");
log.info("Session token " + sessionToken);
if (sessionToken != null) {
AuthenticationObject authentication = new AuthenticationObject();
authentication.setName(sessionToken);
SecurityContextHolder.createEmptyContext().setAuthentication(authentication);
}
filterChain.doFilter(request, response);
}
}
Custom AuthenticationProvider to validate session token and pass Authentication object
#Service
#Slf4j
public class SessionAuthenticationProvider implements AuthenticationProvider{
#Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
log.info("Authentication token " + authentication.getName());
AuthenticationObject authenticationObject = new AuthenticationObject();
authenticationObject.setName("Harsh");
authenticationObject.setAuthenticated(true);
authenticationObject.setAuthorities(Collections.<GrantedAuthority>emptyList());
return authenticationObject;
}
#Override
public boolean supports(Class<?> authentication) {
return AutenticationObject.class.isAssignableFrom(authentication);
}
}
Configuring SecurityFilterChain to secure all my API endpoints and invoking my custom filter. Injecting my custom AuthenticationProvider implementation inside AuthenticationManager.
#Configuration
#EnableWebSecurity
public class SecurityConfiguration {
#Autowired
private SessionTokenFilter sessionTokenFilter;
#Autowired
private SessionAuthenticationProvider authenticationProvider;
#Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeHttpRequests().anyRequest().authenticated().and()
.addFilterBefore(sessionTokenFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
#Bean
public AuthenticationManager authManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder =
http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(authenticationProvider);
return authenticationManagerBuilder.build();
}
}
When I try to invoke any API endpoint I get 403. Below is the trace log of spring security classes
2023-02-17 14:10:22.254[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Securing GET /api/contenteditor/feed/5
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking DisableEncodeUrlFilter (1/12)
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking WebAsyncManagerIntegrationFilter (2/12)
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SecurityContextPersistenceFilter (3/12)
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Did not find SecurityContext in HttpSession 83FA8B6A615DD9898C33EF9B98BCC479 using the SPRING_SECURITY_CONTEXT session attribute
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Created SecurityContextImpl [Null authentication]
[2m2023-02-17 14:10:22.254[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36ms.s.w.c.SecurityContextPersistenceFilter[0;39m [2m:[0;39m Set SecurityContextHolder to empty SecurityContext
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking HeaderWriterFilter (4/12)
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking LogoutFilter (5/12)
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.a.logout.LogoutFilter [0;39m [2m:[0;39m Did not match request to Or [Ant [pattern='/logout', GET], Ant [pattern='/logout', POST], Ant [pattern='/logout', PUT], Ant [pattern='/logout', DELETE]]
[2m2023-02-17 14:10:22.254[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SessionTokenFilter (6/12)
[2m2023-02-17 14:10:22.254[0;39m [32m INFO[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mc.w.presto.security.SessionTokenFilter [0;39m [2m:[0;39m Inside session filter
[2m2023-02-17 14:10:22.255[0;39m [32m INFO[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mc.w.presto.security.SessionTokenFilter [0;39m [2m:[0;39m Session token 12345
[2m2023-02-17 14:10:22.256[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking RequestCacheAwareFilter (7/12)
[2m2023-02-17 14:10:22.256[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.s.HttpSessionRequestCache [0;39m [2m:[0;39m Removing DefaultSavedRequest from session if present
[2m2023-02-17 14:10:22.256[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.s.HttpSessionRequestCache [0;39m [2m:[0;39m Loaded matching saved request http://localhost:6060/presto-boot/api/contenteditor/feed/5
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SecurityContextHolderAwareRequestFilter (8/12)
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking AnonymousAuthenticationFilter (9/12)
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.a.AnonymousAuthenticationFilter [0;39m [2m:[0;39m Set SecurityContextHolder to AnonymousAuthenticationToken [Principal=anonymousUser, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=83FA8B6A615DD9898C33EF9B98BCC479], Granted Authorities=[ROLE_ANONYMOUS]]
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SessionManagementFilter (10/12)
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking ExceptionTranslationFilter (11/12)
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking AuthorizationFilter (12/12)
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mestMatcherDelegatingAuthorizationManager[0;39m [2m:[0;39m Authorizing SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.savedrequest.SavedRequestAwareWrapper#6aa7ef6a]
[2m2023-02-17 14:10:22.257[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mestMatcherDelegatingAuthorizationManager[0;39m [2m:[0;39m Checking authorization on SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.savedrequest.SavedRequestAwareWrapper#6aa7ef6a] using org.springframework.security.authorization.AuthenticatedAuthorizationManager#6e9866e6
[2m2023-02-17 14:10:22.260[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.a.ExceptionTranslationFilter [0;39m [2m:[0;39m Sending AnonymousAuthenticationToken [Principal=anonymousUser, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=83FA8B6A615DD9898C33EF9B98BCC479], Granted Authorities=[ROLE_ANONYMOUS]] to authentication entry point since access is denied
org.springframework.security.access.AccessDeniedException: Access Denied
at org.springframework.security.web.access.intercept.AuthorizationFilter.doFilter(AuthorizationFilter.java:94) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:122) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:116) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:126) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:81) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:109) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:149) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at com.wf.presto.security.SessionTokenFilter.doFilterInternal(SessionTokenFilter.java:31) ~[classes/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:112) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:82) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.session.DisableEncodeUrlFilter.doFilterInternal(DisableEncodeUrlFilter.java:42) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:221) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:186) ~[spring-security-web-5.7.6.jar:5.7.6]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:354) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:267) ~[spring-web-5.3.24.jar:5.3.24]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.70.jar:9.0.70]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
[2m2023-02-17 14:10:22.260[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.s.HttpSessionRequestCache [0;39m [2m:[0;39m Saved request http://localhost:6060/presto-boot/api/contenteditor/feed/5 to session
[2m2023-02-17 14:10:22.260[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.a.Http403ForbiddenEntryPoint [0;39m [2m:[0;39m Pre-authenticated entry point called. Rejecting access
[2m2023-02-17 14:10:22.260[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.header.writers.HstsHeaderWriter [0;39m [2m:[0;39m Not injecting HSTS header since it did not match request to [Is Secure]
[2m2023-02-17 14:10:22.260[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Did not store empty SecurityContext
[2m2023-02-17 14:10:22.260[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Did not store empty SecurityContext
[2m2023-02-17 14:10:22.260[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36ms.s.w.c.SecurityContextPersistenceFilter[0;39m [2m:[0;39m Cleared SecurityContextHolder to complete request
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Trying to match request against DefaultSecurityFilterChain [RequestMatcher=any request, Filters=[org.springframework.security.web.session.DisableEncodeUrlFilter#4ae2b686, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#4b15617a, org.springframework.security.web.context.SecurityContextPersistenceFilter#37cbe1e9, org.springframework.security.web.header.HeaderWriterFilter#2b6c24bf, org.springframework.security.web.authentication.logout.LogoutFilter#1632f898, com.wf.presto.security.SessionTokenFilter#b4e0b59, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#6a05b84a, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#20862069, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#ccfc0c1, org.springframework.security.web.session.SessionManagementFilter#4b82b88a, org.springframework.security.web.access.ExceptionTranslationFilter#46736c06, org.springframework.security.web.access.intercept.AuthorizationFilter#40a632cb]] (1/1)
[2m2023-02-17 14:10:22.261[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Securing GET /error
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking DisableEncodeUrlFilter (1/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking WebAsyncManagerIntegrationFilter (2/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SecurityContextPersistenceFilter (3/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Did not find SecurityContext in HttpSession 83FA8B6A615DD9898C33EF9B98BCC479 using the SPRING_SECURITY_CONTEXT session attribute
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Created SecurityContextImpl [Null authentication]
[2m2023-02-17 14:10:22.261[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36ms.s.w.c.SecurityContextPersistenceFilter[0;39m [2m:[0;39m Set SecurityContextHolder to empty SecurityContext
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking HeaderWriterFilter (4/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking LogoutFilter (5/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.a.logout.LogoutFilter [0;39m [2m:[0;39m Did not match request to Or [Ant [pattern='/logout', GET], Ant [pattern='/logout', POST], Ant [pattern='/logout', PUT], Ant [pattern='/logout', DELETE]]
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SessionTokenFilter (6/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking RequestCacheAwareFilter (7/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.s.HttpSessionRequestCache [0;39m [2m:[0;39m Did not match request /error to the saved one DefaultSavedRequest [http://localhost:6060/presto-boot/api/contenteditor/feed/5]
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SecurityContextHolderAwareRequestFilter (8/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking AnonymousAuthenticationFilter (9/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.s.w.a.AnonymousAuthenticationFilter [0;39m [2m:[0;39m Set SecurityContextHolder to AnonymousAuthenticationToken [Principal=anonymousUser, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=83FA8B6A615DD9898C33EF9B98BCC479], Granted Authorities=[ROLE_ANONYMOUS]]
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking SessionManagementFilter (10/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking ExceptionTranslationFilter (11/12)
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Invoking AuthorizationFilter (12/12)
[2m2023-02-17 14:10:22.261[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mo.s.security.web.FilterChainProxy [0;39m [2m:[0;39m Secured GET /error
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mestMatcherDelegatingAuthorizationManager[0;39m [2m:[0;39m Authorizing org.springframework.security.web.FilterInvocation$DummyRequest#4f725109
[2m2023-02-17 14:10:22.261[0;39m [32mTRACE[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mestMatcherDelegatingAuthorizationManager[0;39m [2m:[0;39m Checking authorization on org.springframework.security.web.FilterInvocation$DummyRequest#4f725109 using org.springframework.security.authorization.AuthenticatedAuthorizationManager#6e9866e6
[2m2023-02-17 14:10:22.261[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Did not store anonymous SecurityContext
[2m2023-02-17 14:10:22.261[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36mw.c.HttpSessionSecurityContextRepository[0;39m [2m:[0;39m Did not store anonymous SecurityContext
[2m2023-02-17 14:10:22.261[0;39m [32mDEBUG[0;39m [35m23496[0;39m [2m---[0;39m [2m[nio-6060-exec-2][0;39m [36ms.s.w.c.SecurityContextPersistenceFilter[0;39m [2m:[0;39m Cleared SecurityContextHolder to complete request

Found a solution. Removed authenticationprovider from SecurityConfiguration class I have created.
Made changes in my SessionTokenFilter class
#Component
#Slf4j
public class SessionTokenFilter extends OncePerRequestFilter {
#Autowired
private AuthenticationProvider authenticationProvider;
#Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
log.info("Inside session filter");
String sessionToken = request.getHeader("session-token");
log.info("Session token " + sessionToken);
if (sessionToken != null) {
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(sessionToken, "Pwd");
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authenticationProvider.authenticate(authentication));
SecurityContextHolder.setContext(context);
}
filterChain.doFilter(request, response);
}
}

Related

Whitelabel Error Page while run simple service

I have created Spring Starter Project in STS (Spring Tool Suite) with simple Controller:
#RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
#GetMapping("/greeting")
public Greeting greeting(#RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
as shown in tutorial.
I run service from IDE and it starts fine. But when I go to http://localhost:8080/greeting?name=User I get error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Oct 01 10:25:10 PDT 2020
There was an unexpected error (type=Not Found, status=404).
Why I'm getting this error? How to fix that?
UPD
After adding more log I got mapping to error:
2020-10-02 06:17:46.078[0;39m [32mDEBUG[0;39m [35m4851[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m 2 mappings in 'requestMappingHandlerMapping'
[2m2020-10-02 06:17:46.136[0;39m [32m INFO[0;39m [35m4851[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2020-10-02 06:17:46.142[0;39m [32m INFO[0;39m [35m4851[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.example.demo.Demo1Application [0;39m [2m:[0;39m Started Demo1Application in 1.221 seconds (JVM running for 1.934)
[2m2020-10-02 06:17:51.491[0;39m [32m INFO[0;39m [35m4851[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2020-10-02 06:17:51.493[0;39m [32m INFO[0;39m [35m4851[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2020-10-02 06:17:51.496[0;39m [32m INFO[0;39m [35m4851[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Completed initialization in 3 ms
[2m2020-10-02 06:17:51.508[0;39m [32mTRACE[0;39m [35m4851[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m 2 matching mappings: [{ /error, produces [text/html]}, { /error}]
[2m2020-10-02 06:17:51.509[0;39m [32mTRACE[0;39m [35m4851[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
If you get 404 then obviously your controller is not registered. It is difficult to help you if we are missing other source code.
Maybe your controller is in foreign package. Spring boot use component scan by default for all packages (and subpackages) of your main Spring boot class (class with #SpringBootApplication annotation).
To find issue, just add
logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=TRACE
to your application.properties and search for greeting string in logs
you should find something like
2020-10-01 19:49:12.610 TRACE 36840 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping :
c.t.t.c.RootController:
{GET /}: welcome()
{GET /greeting}: greeting(Model)

java.lang.NullPointerException while running Springboot application for Kafka

I am trying to run Springboot application but getting below error. I tried to search for this error but couldn't get the exact resolution. As this is a Kafka consumer application, I am hoping to get the records from the consumed topic. But, the application is failing at the first step only but I am not only to figure out the issue. As I am new to Springboot, any suggestions for debugging would be appreciated.
Error :
[2m2020-07-03 18:07:22.278[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mcom.example.consumer.ApplicationRun [0;39m [2m:[0;39m Starting ApplicationRun on LHTU05CD9032TMM with PID 36708 (C:\Users\psingh69\IdeaProjects\KafkaSpecificAvroConsumer\target\classes started by psingh69 in C:\Users\psingh69\IdeaProjects\KafkaSpecificAvroConsumer)
[2m2020-07-03 18:07:22.287[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mcom.example.consumer.ApplicationRun [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2020-07-03 18:07:22.386[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[2m2020-07-03 18:07:22.386[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[2m2020-07-03 18:07:25.300[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2020-07-03 18:07:25.341[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2020-07-03 18:07:25.342[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.24]
[2m2020-07-03 18:07:26.199[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2020-07-03 18:07:26.199[0;39m [32mDEBUG[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.web.context.ContextLoader [0;39m [2m:[0;39m Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
[2m2020-07-03 18:07:26.199[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.web.context.ContextLoader [0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 3813 ms
[2m2020-07-03 18:07:26.845[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mf.a.AutowiredAnnotationBeanPostProcessor[0;39m [2m:[0;39m Autowired annotation is not supported on static fields: static com.example.consumer.PayConsumer com.example.consumer.ApplicationRun.payConsumer
[2m2020-07-03 18:07:26.945[0;39m [32mDEBUG[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Patterns [/**/favicon.ico] in 'faviconHandlerMapping'
[2m2020-07-03 18:07:27.249[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.s.concurrent.ThreadPoolTaskExecutor [0;39m [2m:[0;39m Initializing ExecutorService 'applicationTaskExecutor'
[2m2020-07-03 18:07:27.269[0;39m [32mDEBUG[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerAdapter[0;39m [2m:[0;39m ControllerAdvice beans: 0 #ModelAttribute, 0 #InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
[2m2020-07-03 18:07:27.363[0;39m [32mDEBUG[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m 2 mappings in 'requestMappingHandlerMapping'
[2m2020-07-03 18:07:27.402[0;39m [32mDEBUG[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
[2m2020-07-03 18:07:27.418[0;39m [32mDEBUG[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36m.m.m.a.ExceptionHandlerExceptionResolver[0;39m [2m:[0;39m ControllerAdvice beans: 0 #ExceptionHandler, 1 ResponseBodyAdvice
[2m2020-07-03 18:07:27.884[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.d.a.OptionalLiveReloadServer [0;39m [2m:[0;39m LiveReload server is running on port 35729
[2m2020-07-03 18:07:27.894[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.a.e.web.EndpointLinksResolver [0;39m [2m:[0;39m Exposing 2 endpoint(s) beneath base path '/actuator'
[2m2020-07-03 18:07:28.025[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2020-07-03 18:07:28.032[0;39m [32m INFO[0;39m [35m36708[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mcom.example.consumer.ApplicationRun [0;39m [2m:[0;39m Started ApplicationRun in 6.579 seconds (JVM running for 10.534)
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
at com.example.consumer.ApplicationRun.main(ApplicationRun.java:19)
... 5 more
Application.java :
package com.example.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ApplicationRun {
#Autowired
static PayConsumer payConsumer;
public static void main(String[] args) {
SpringApplication.run(ApplicationRun.class, args);
payConsumer.run();
}
}
This worked for me !!
package com.example.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ApplicationRun {
#Autowired
static PayConsumer payConsumer;
public ApplicationRun(PayConsumer payConsumer) {
ApplicationRun.payConsumer=payConsumer;
}
public static void main(String[] args) {
SpringApplication.run(ApplicationRun.class, args);
payConsumer.run();
}
}

Spring Boot HttpSecurity always 403 forbidden

I always get http status 403. I have this security configuration:
#Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers("/api/users/login/").permitAll()
.anyRequest().authenticated();
}
#Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("*"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
I cannot post to /api/users/login
2019-10-15 12:25:49.567[0;39m [32mDEBUG[0;39m [35m7423[0;39m
[2m---[0;39m [2m[nio-8080-exec-1][0;39m
[36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m
"ERROR" dispatch for POST "/error", parameters={} [2m2019-10-15
12:25:49.576[0;39m [32mDEBUG[0;39m [35m7423[0;39m [2m---[0;39m
[2m[nio-8080-exec-1][0;39m
[36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m
Mapped to public
org.springframework.http.ResponseEntity>
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2m2019-10-15 12:25:49.605[0;39m [32mDEBUG[0;39m [35m7423[0;39m
[2m---[0;39m [2m[nio-8080-exec-1][0;39m
[36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m
Using 'application/json', given [/] and supported [application/json,
application/+json, application/json, application/+json]
[2m2019-10-15 12:25:49.608[0;39m [32mDEBUG[0;39m [35m7423[0;39m
[2m---[0;39m [2m[nio-8080-exec-1][0;39m
[36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m
Writing [{timestamp=Tue Oct 15 12:25:49 CEST 2019, status=403,
error=Forbidden, message=Access Denied, path=/ (truncated)...]
[2m2019-10-15 12:25:49.661[0;39m [32mDEBUG[0;39m [35m7423[0;39m
[2m---[0;39m [2m[nio-8080-exec-1][0;39m
[36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m
Exiting from "ERROR" dispatch, status 403
Try .antMatchers(HttpMethod.POST,"/api/users/login").permitAll(), also note that you have .antMatchers("/api/users/login/") and you are makin an request to /api/users/login note extra / in your antMatchers.
You can also use configure(WebSecurity web) which will bypass the Spring Security filter chain as described here

Spring Actuator does not provide entry points

I am trying to get extra information about an spring application using Actuator. So, here is what i added to my dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
When i run my application i got the following endpoints:
[2m2017-09-27 02:35:24.618[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'metricsFilter' to: [/*]
[2m2017-09-27 02:35:24.619[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'characterEncodingFilter' to: [/*]
[2m2017-09-27 02:35:24.619[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
[2m2017-09-27 02:35:24.619[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'httpPutFormContentFilter' to: [/*]
[2m2017-09-27 02:35:24.619[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'requestContextFilter' to: [/*]
[2m2017-09-27 02:35:24.619[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'webRequestLoggingFilter' to: [/*]
[2m2017-09-27 02:35:24.619[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'applicationContextIdFilter' to: [/*]
[2m2017-09-27 02:35:25.389[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerAdapter[0;39m [2m:[0;39m Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#e94dcf: startup date [Wed Sep 27 02:35:21 GMT-12:00 2017]; root of context hierarchy
[2m2017-09-27 02:35:25.495[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/hey]}" onto public java.lang.String com.example.demo.HelloController.respond()
[2m2017-09-27 02:35:25.497[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/send]}" onto public java.lang.String com.example.demo.MailController.send()
[2m2017-09-27 02:35:25.501[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2m2017-09-27 02:35:25.501[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
[2m2017-09-27 02:35:25.587[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-09-27 02:35:25.587[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-09-27 02:35:25.649[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-09-27 02:35:26.203[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
[2m2017-09-27 02:35:26.203[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.204[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.205[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.207[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
[2m2017-09-27 02:35:26.208[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
[2m2017-09-27 02:35:26.208[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.209[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.211[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.212[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
[2m2017-09-27 02:35:26.213[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.214[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
[2m2017-09-27 02:35:26.215[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
[2m2017-09-27 02:35:26.215[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.216[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
[2m2017-09-27 02:35:26.217[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
[2m2017-09-27 02:35:26.217[0;39m [32m INFO[0;39m [35m5128[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.e.mvc.EndpointHandlerMapping [0;39m [2m:[0;39m Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke
So, i would like to know what are the beans using the endpoint
http://localhost:8080/beans
but it complains with:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Sep 27 02:41:33 GMT-12:00 2017
There was an unexpected error (type=Unauthorized, status=401).
Full authentication is required to access this resource.
So, what is wrong?
I don't have any security plugin in my code
Although you wrote:
I don't have any security plugin in my code
As long as spring-security is on the classpath the actuator endpoints may be secured.
So either:
Ensure that Spring Security is not on the classpath
Or
Set this application property:
management.security.enabled=false
It has security by default. You can disable it in the application properties.
Please see: Unauthorized in spring boot admin
There are two way you can remove this error.
1 Way -> Update your #EnableAutoConfiguration annotation with below.
#EnableAutoConfiguration(exclude = {
org.activiti.spring.boot.RestApiAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.activiti.spring.boot.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration.class
})
2 Way -> Follow this to disable authentication.

SpringBoot + Hibernate + Postgres

I am trying to insert a value(http://localhost:8080/create?name=name1&email=name1#email.com) to Postgres DB using SpringBoot and hibernate but in log it says as it inserts but no records are inserted also no exception is there.Please help me to find the issue.
Postgres Version:
"PostgreSQL 9.6.3 on x86_64-pc-mingw64, compiled by gcc.exe (Rev5, Built by MSYS2 project) 4.9.2, 64-bit"
build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
group = 'netgloo'
version = '0.0.1-SNAPSHOT'
description = """spring-boot-mysql-springdatajpa-hibernate"""
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.5.2.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.5.2.RELEASE'
compile group: 'postgres', name: 'postgres', version:'42.1.1.jre6'
}
User.java:
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#NotNull
private String email;
#NotNull
private String name;
public User() { }
public User(long id) {
this.id = id;
}
public User(String email, String name) {
this.email = email;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long value) {
this.id = value;
}
public String getEmail() {
return email;
}
public void setEmail(String value) {
this.email = value;
}
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
}
Controller:
#Controller
public class UserController {
#RequestMapping("/create")
#ResponseBody
public String create(String email, String name) {
User user = null;
try {
user = new User(email, name);
userDao.save(user);
}
catch (Exception ex) {
return "Error creating the user: " + ex.toString();
}
return "User succesfully created! (id = " + user.getId() + ")";
}
#RequestMapping("/get-by-email")
#ResponseBody
public String getByEmail(String email) {
String userId;
try {
User user = userDao.findByEmail(email);
userId = String.valueOf(user.getId());
}
catch (Exception ex) {
return "User not found";
}
return "The user id is: " + userId;
}
}
UserDao:
#Transactional
public interface UserDao extends CrudRepository<User, Long> {
public User findByEmail(String email);
} // class UserDao
logs:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v1.5.2.RELEASE)[0;39m
[2m2017-07-07 14:54:59.528[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mnetgloo.Application [0;39m [2m:[0;39m Starting Application on B20GPF2 with PID 17176 (C:\Users\a591470\projects\mylearning\spring-boot-samples-master\spring-boot-mysql-springdatajpa-hibernate\bin started by a591470 in C:\Users\a591470\projects\mylearning\spring-boot-samples-master\spring-boot-mysql-springdatajpa-hibernate)
[2m2017-07-07 14:54:59.533[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mnetgloo.Application [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2017-07-07 14:54:59.652[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mationConfigEmbeddedWebApplicationContext[0;39m [2m:[0;39m Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#4387b79e: startup date [Fri Jul 07 14:54:59 IST 2017]; root of context hierarchy
[2m2017-07-07 14:55:01.391[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mtrationDelegate$BeanPostProcessorChecker[0;39m [2m:[0;39m Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$61eecdd6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2m2017-07-07 14:55:01.919[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.b.c.e.t.TomcatEmbeddedServletContainer[0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2017-07-07 14:55:01.935[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service Tomcat
[2m2017-07-07 14:55:01.936[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet Engine: Apache Tomcat/8.5.11
[2m2017-07-07 14:55:02.170[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2017-07-07 14:55:02.171[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.web.context.ContextLoader [0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 2525 ms
[2m2017-07-07 14:55:02.466[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.ServletRegistrationBean [0;39m [2m:[0;39m Mapping servlet: 'dispatcherServlet' to [/]
[2m2017-07-07 14:55:02.472[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'characterEncodingFilter' to: [/*]
[2m2017-07-07 14:55:02.472[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
[2m2017-07-07 14:55:02.472[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'httpPutFormContentFilter' to: [/*]
[2m2017-07-07 14:55:02.472[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.w.servlet.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'requestContextFilter' to: [/*]
[2m2017-07-07 14:55:04.067[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Building JPA container EntityManagerFactory for persistence unit 'default'
[2m2017-07-07 14:55:04.103[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [
name: default
...]
[2m2017-07-07 14:55:04.304[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.Version [0;39m [2m:[0;39m HHH000412: Hibernate Core {5.0.12.Final}
[2m2017-07-07 14:55:04.306[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.cfg.Environment [0;39m [2m:[0;39m HHH000206: hibernate.properties not found
[2m2017-07-07 14:55:04.309[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.cfg.Environment [0;39m [2m:[0;39m HHH000021: Bytecode provider name : javassist
[2m2017-07-07 14:55:04.388[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.hibernate.annotations.common.Version [0;39m [2m:[0;39m HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
[2m2017-07-07 14:55:04.602[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.dialect.Dialect [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
[2m2017-07-07 14:55:04.920[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.h.e.j.e.i.LobCreatorBuilderImpl [0;39m [2m:[0;39m HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
[2m2017-07-07 14:55:04.923[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.type.BasicTypeRegistry [0;39m [2m:[0;39m HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType#2f84acf7
[2m2017-07-07 14:55:05.243[0;39m [33m WARN[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.orm.deprecation [0;39m [2m:[0;39m HHH90000014: Found use of deprecated [org.hibernate.id.SequenceGenerator] sequence-based id generator; use org.hibernate.id.enhanced.SequenceStyleGenerator instead. See Hibernate Domain Model Mapping Guide for details.
[2m2017-07-07 14:55:05.724[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaUpdate [0;39m [2m:[0;39m HHH000228: Running hbm2ddl schema update
[2m2017-07-07 14:55:05.877[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2017-07-07 14:55:06.920[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerAdapter[0;39m [2m:[0;39m Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#4387b79e: startup date [Fri Jul 07 14:54:59 IST 2017]; root of context hierarchy
[2m2017-07-07 14:55:07.038[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/]}" onto public java.lang.String netgloo.controllers.MainController.index()
[2m2017-07-07 14:55:07.040[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/delete]}" onto public java.lang.String netgloo.controllers.UserController.delete(long)
[2m2017-07-07 14:55:07.040[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/create]}" onto public java.lang.String netgloo.controllers.UserController.create(java.lang.String,java.lang.String) throws java.lang.Exception
[2m2017-07-07 14:55:07.041[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/update]}" onto public java.lang.String netgloo.controllers.UserController.updateUser(long,java.lang.String,java.lang.String)
[2m2017-07-07 14:55:07.041[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/get-by-email]}" onto public java.lang.String netgloo.controllers.UserController.getByEmail(java.lang.String) throws java.lang.Exception
[2m2017-07-07 14:55:07.044[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2m2017-07-07 14:55:07.045[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
[2m2017-07-07 14:55:07.113[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-07-07 14:55:07.113[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-07-07 14:55:07.172[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-07-07 14:55:07.557[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter [0;39m [2m:[0;39m Registering beans for JMX exposure on startup
[2m2017-07-07 14:55:08.132[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.b.c.e.t.TomcatEmbeddedServletContainer[0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http)
[2m2017-07-07 14:55:08.138[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[ main][0;39m [36mnetgloo.Application [0;39m [2m:[0;39m Started Application in 9.279 seconds (JVM running for 11.078)
[2m2017-07-07 14:55:22.454[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring FrameworkServlet 'dispatcherServlet'
[2m2017-07-07 14:55:22.454[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization started
[2m2017-07-07 14:55:22.692[0;39m [32m INFO[0;39m [35m17176[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization completed in 237 ms
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into users (email, name, id) values (?, ?, ?)
When you use a serial or a bigserial field in a Postgre table, it is an auto-increment field for which a sequence is automatically created.
This can cause trouble with the sequence used by hibernate by default (hibernate_sequence) when using GenerationType.AUTO.
To avoid that, you may either rely on the id generator of your serial field :
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
Or you may find the sequence (1) that the bigserial has generated (let's call it user_id_seq for this example), and specify it in your annotations so that hibernate uses this one directly :
#Id
#SequenceGenerator(name = "seqUser", sequenceName = "user_id_seq", allocationSize = 1)
#GeneratedValue(generator = "seqUser", strategy = GenerationType.AUTO)
(1): in the table script, you probably have something like the following, telling you the name of the sequence used by Postgre for the increment (usually it is like : table name_column name_seq).
id bigint NOT NULL DEFAULT nextval('user_id_seq'::regclass)
Some more information about serial fields can be found in this question : PostgreSQL bigserial & nextval
Ans some information about GenerationType strategies with Postgre : JPA and PostgreSQL with GenerationType.IDENTITY
create a service class and add your method to call Dao(Repo ) and make sure that
the method is transactional
then from controller call the service instead of calling the dao

Categories