Spring Security getPrincipal () Method Returns anonymousUser

in my spring web application I want to get authenticated user in controller:

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 

But the main meaning is "anonymousUser", although I logged in. How can I get an authenticated user? My configurations in spring -security.xml:

 <http auto-config="true" request-matcher="regex"> <intercept-url pattern="/welcome*" access="ROLE_USER" /> <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed" /> <logout logout-success-url="/logout" /> </http> 
+6
source share
1 answer

Not sure what I understand, but try this

 <http auto-config="true" request-matcher="regex"> <intercept-url pattern="/welcome*" access="ROLE_USER" /> <intercept-url pattern="/*" access="IS_AUTHENTICATED,IS_AUTHENTICATED_ANONYMOUSLY"/> 
+4
source

All Articles