Bonjour,
Je souhaiterais récupérer l'utilisateur loggé dans un controller (spring) depuis une requête ajax. Le problème est que je ne récupère que "anonymousUser"...
J'ai essayé de le via :
Code :
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
- String username = authentication.getName();
- User user = userRepository.findUserByPseudo(username);
|
Le spring-security.xml si besoin :
Code :
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:security="http://www.springframework.org/schema/security"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/sch [...] ns-3.2.xsd
- http://www.springframework.org/schema/security
- http://www.springframework.org/sch [...] ">
-
- <bean id="simpleUrlAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
- <property name="defaultTargetUrl" value="/home"/>
- <property name="targetUrlParameter" value="spring-security-redirect"/>
- </bean>
-
- <security:http pattern="/resources/**" security="none"/>
-
- <security:http auto-config="true" use-expressions="true" create-session="always" >
- <security:intercept-url pattern="/index*" access="permitAll" />
- <security:intercept-url pattern="/film/ajouter-un-film*" access="isAuthenticated()" />
-
-
- <security:form-login
- authentication-success-handler-ref="simpleUrlAuthenticationSuccessHandler"
- authentication-failure-url="/home" />
- <security:logout logout-success-url="/" logout-url="/logout" />
- </security:http>
-
- <security:authentication-manager>
- <security:authentication-provider>
- <security:jdbc-user-service data-source-ref="dataSource"
- users-by-username-query="select login, password, valide from users where login=?"
- authorities-by-username-query="select us.login, ro.roleName from users us, roles ro, users_roles ru
- where us.id = ru.users_id AND ro.id = ru.roles_id AND us.login =?"
- />
- </security:authentication-provider>
- </security:authentication-manager>
- </beans>
|
Merci d'avance pour votre aide.