I am developing a Spring REST application using Spring Boot. I am consuming the Spring REST APIs using angular JS client.
The stack I am using is :
Spring Boot 1.4.2 , Spring Security , Angular JS , Tomcat 8
My Issue Is :
1)At login , user gets succesfully authenticated. Custom authentication success handler returns 200 status code.
2)After authentication success , when clint sends another request to access a procted resource , HttpSession is NULL
3)Due to which when SecurityContextPersistenceFilter
tries to retrieve the SecurityContext
from HttpSession
it is returned as null and
and again server sends request to /login
resource
4)So my questions are :
1)Why is httpsesion is null for second request ?
2)I have observed that Spring security returns JSESSIONID
as cookie after first succesfull authentication.
3)But after that , client is NOT sending this JSESSIONID
in request hence second request won't be in same session.
4)If that is the case , how is SecurityContext
will be retrieved if SESSION
is not established ?
Please help as I am not able to proceed here
EDIT 1 :
I am using default in memory user provided by spring security. My security configuration is bellow :
@Configuration
@EnableWebSecurity
@ComponentScan({"com.atul.security"})
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().successHandler(authenticationSuccessHandler)
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS,"/*").permitAll()
.antMatchers("/index.html", "/home.html", "/login.html", "/").permitAll()
.anyRequest().authenticated()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
I am getting below user as authenticated user :
org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc:
Principal: anonymousUser; Credentials: [PROTECTED];
Authenticated: true;
Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364:
RemoteIpAddress: 0:0:0:0:0:0:0:1;
SessionId: null;
Granted Authorities: ROLE_ANONYMOUS
The request is now failing at : AbstractSecurityInterceptor
: AccessDecisionVoter
returning false for authenticated
expression and access denied exception is thrown