Jestem nowicjuszem w Spring Security 3. Używam ról, aby użytkownicy mogli się zalogować.Jak dodać niestandardowy filtr po autoryzacji użytkownika w aplikacji wiosna
Chcę dodać wartość sesji po autoryzacji użytkownika do aplikacji. Może potrzebuję jakiegoś filtra, aby przekierował mnie do mojej metody, która dodaje pewną wartość sesji. Skonfigurowałem plik security.xml, ale nie jestem pewien, czy robię właściwe rzeczy. Pomocne mogą być wszelkie przykłady w tym kierunku. Którą klasę filtra należy użyć? Jak skonfigurować plik security.xml?
<custom-filter ref="authenticationFilter" after="FORM_LOGIN_FILTER "/>
<beans:bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="filterProcessesUrl" value="/j_spring_security_check" />
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="authenticationSuccessHandler" ref="successHandler" />
</beans:bean>
<beans:bean id="successHandler" class="org.dfci.sparks.datarequest.security.CustomAuthorizationFilter"/>
Moja metoda klasy filtra Potrzebuję dodać wartość sesji.
public class CustomAuthorizationFilter implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
Set<String> roles = AuthorityUtils.authorityListToSet(authentication
.getAuthorities());
if (roles.contains("ROLE_USER")) {
request.getSession().setAttribute("myVale", "myvalue");
}
}
}
Edycja kodu
I zostały zmodyfikowane mój plik security.xml i klasa plik
<custom-filter ref="authenticationFilter" after="FORM_LOGIN_FILTER "/>
public class CustomAuthorizationFilter extends GenericFilterBean {
/*
* ServletRequestAttributes attr = (ServletRequestAttributes)
* RequestContextHolder.currentRequestAttributes(); HttpSession
* session=attr.getRequest().getSession(true);
*/
@Autowired
private UserService userService;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession(true);
Authentication authentication = SecurityContextHolder
.getContext().getAuthentication();
Set<String> roles = AuthorityUtils
.authorityListToSet(authentication.getAuthorities());
User user = null;
if (true) {
session.setAttribute("Flag", "Y");
}
}
} catch (IOException ex) {
throw ex;
}
}
}
Które wywołuje za każdym URL. Czy jest to jakakolwiek alternatywa dla metody filtrowania połączeń tylko raz, gdy użytkownik jest uwierzytelniany?
Na czym polega problem z powyższym kodem? Czy nie otrzymujesz atrybutu myValue sesji w adresie URL sukcesu logowania? –
@SunilChavan: Wywołuje ten filtr, zanim użytkownik zostanie uwierzytelniony. – Raje
Jak możesz tak mówić? Zobacz szczegóły (API) [http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/AuthenticationSuccessHandler.html]. Wyraźnie mówi, że po udanym uwierzytelnieniu wywołuje metodę onAuthenticationSuccess. –