I have a web project with Spring Security, and I tried to save the cookie in a method that handles authentication success. However, when I look at browser cookies, only JSESSIONID appears, and the same thing happens when I look at request.getCookies () in the servlet that Spring redirects.
I tried to save the cookie in one of the servlets of the application and the cookie will be saved correctly, so maybe Spring Security will clear the response. Do you have any ideas?
One way would be to save it in the session, and then get it and save the cookie on the servlet to which the login is redirected. Another option is to save a cookie with javascript, for example. But I do not like these decisions. thanks in advance
Here is the relevant code:
public class RoleBasedAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler implements
AuthenticationSuccessHandler {
...
Map<String, String[]> parameterMap = request.getParameterMap();
if (parameterMap.containsKey("language")) {
saveCookie("language", parameterMap.get("language")[0], response);
}
}
public static void saveCookie(String cookieName, String value, HttpServletResponse response) {
Cookie cookie = new Cookie(cookieName, value);
cookie.setMaxAge(2592000);
cookie.setDomain("projectName");
cookie.setPath("/");
response.addCookie(cookie);
}
}
<security:http auto-config="false" ...>
<security:form-login login-page="/login.do" authentication-success-handler-ref="redirectRoleStrategy" .../>
...
</security:http>
<bean id="redirectRoleStrategy" class="com.companyName.security.RoleBasedAuthenticationSuccessHandler">
<beans:property name="roleUrlMap">
<beans:map>
<beans:entry key="ROLE_ADMIN" value="/privat/application.do"/>
...
</beans:map>
</beans:property>
</bean>
source
share