SecurityContextHolder.getContext().getAuthentication() always return 'anonymousUser'
up vote
1
down vote
favorite
I created Spring boot application with the following configuration:
- Spring boot 2.1.0.RELEASE
- OpenJdk 11
I have an AuditConfiguration
class in my project that looks like:
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditConfiguration {
@Bean
public AuditorAware<String> auditorProvider() {
return new AuditorAwareImpl();
}
class AuditorAwareImpl implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
Principal principal =
SecurityContextHolder.getContext().getAuthentication();
return Optional.of(principal.getName());
}
}
}
and SecurityContextHolder.getContext().getAuthentication()
always returns anonymousUser
.
However, the following code returns the correct user name.
@RestController
@RequestMapping("/history")
public class HistoryEndpoint {
@RequestMapping(value = "/username", method = RequestMethod.GET)
@ResponseBody
public String currentUserName(Principal principal) {
return principal.getName();
}
}
I need your help for resolving this issue.
spring spring-boot spring-security
New contributor
add a comment |
up vote
1
down vote
favorite
I created Spring boot application with the following configuration:
- Spring boot 2.1.0.RELEASE
- OpenJdk 11
I have an AuditConfiguration
class in my project that looks like:
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditConfiguration {
@Bean
public AuditorAware<String> auditorProvider() {
return new AuditorAwareImpl();
}
class AuditorAwareImpl implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
Principal principal =
SecurityContextHolder.getContext().getAuthentication();
return Optional.of(principal.getName());
}
}
}
and SecurityContextHolder.getContext().getAuthentication()
always returns anonymousUser
.
However, the following code returns the correct user name.
@RestController
@RequestMapping("/history")
public class HistoryEndpoint {
@RequestMapping(value = "/username", method = RequestMethod.GET)
@ResponseBody
public String currentUserName(Principal principal) {
return principal.getName();
}
}
I need your help for resolving this issue.
spring spring-boot spring-security
New contributor
Try autowire principal object instead of getting it directly from security context holder and check if issue gone.
– Kamil W.
2 days ago
are you setting Authentication object into spring security context?
– Raheela Aslam
2 days ago
Please show your configuration files
– shazin
2 days ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I created Spring boot application with the following configuration:
- Spring boot 2.1.0.RELEASE
- OpenJdk 11
I have an AuditConfiguration
class in my project that looks like:
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditConfiguration {
@Bean
public AuditorAware<String> auditorProvider() {
return new AuditorAwareImpl();
}
class AuditorAwareImpl implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
Principal principal =
SecurityContextHolder.getContext().getAuthentication();
return Optional.of(principal.getName());
}
}
}
and SecurityContextHolder.getContext().getAuthentication()
always returns anonymousUser
.
However, the following code returns the correct user name.
@RestController
@RequestMapping("/history")
public class HistoryEndpoint {
@RequestMapping(value = "/username", method = RequestMethod.GET)
@ResponseBody
public String currentUserName(Principal principal) {
return principal.getName();
}
}
I need your help for resolving this issue.
spring spring-boot spring-security
New contributor
I created Spring boot application with the following configuration:
- Spring boot 2.1.0.RELEASE
- OpenJdk 11
I have an AuditConfiguration
class in my project that looks like:
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditConfiguration {
@Bean
public AuditorAware<String> auditorProvider() {
return new AuditorAwareImpl();
}
class AuditorAwareImpl implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
Principal principal =
SecurityContextHolder.getContext().getAuthentication();
return Optional.of(principal.getName());
}
}
}
and SecurityContextHolder.getContext().getAuthentication()
always returns anonymousUser
.
However, the following code returns the correct user name.
@RestController
@RequestMapping("/history")
public class HistoryEndpoint {
@RequestMapping(value = "/username", method = RequestMethod.GET)
@ResponseBody
public String currentUserName(Principal principal) {
return principal.getName();
}
}
I need your help for resolving this issue.
spring spring-boot spring-security
spring spring-boot spring-security
New contributor
New contributor
edited 2 days ago
shazin
14.8k23145
14.8k23145
New contributor
asked 2 days ago
bhagya perera
62
62
New contributor
New contributor
Try autowire principal object instead of getting it directly from security context holder and check if issue gone.
– Kamil W.
2 days ago
are you setting Authentication object into spring security context?
– Raheela Aslam
2 days ago
Please show your configuration files
– shazin
2 days ago
add a comment |
Try autowire principal object instead of getting it directly from security context holder and check if issue gone.
– Kamil W.
2 days ago
are you setting Authentication object into spring security context?
– Raheela Aslam
2 days ago
Please show your configuration files
– shazin
2 days ago
Try autowire principal object instead of getting it directly from security context holder and check if issue gone.
– Kamil W.
2 days ago
Try autowire principal object instead of getting it directly from security context holder and check if issue gone.
– Kamil W.
2 days ago
are you setting Authentication object into spring security context?
– Raheela Aslam
2 days ago
are you setting Authentication object into spring security context?
– Raheela Aslam
2 days ago
Please show your configuration files
– shazin
2 days ago
Please show your configuration files
– shazin
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
bhagya perera is a new contributor. Be nice, and check out our Code of Conduct.
bhagya perera is a new contributor. Be nice, and check out our Code of Conduct.
bhagya perera is a new contributor. Be nice, and check out our Code of Conduct.
bhagya perera is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203519%2fsecuritycontextholder-getcontext-getauthentication-always-return-anonymousu%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Try autowire principal object instead of getting it directly from security context holder and check if issue gone.
– Kamil W.
2 days ago
are you setting Authentication object into spring security context?
– Raheela Aslam
2 days ago
Please show your configuration files
– shazin
2 days ago