Jak dodać niestandardowy filtr po autoryzacji użytkownika w aplikacji wiosennej

Jestem nowicjuszem w Spring Security 3. Używam ról dla użytkowników do logowania.

Chcę dodać wartość sesji po autoryzacji użytkownika do aplikacji. Może potrzebuję jakiegoś filtra, żeby przekierował do mojej metody, która dodaje pewną wartość sesji. Skonfigurowałem plik security.xml, ale nie jestem pewien, czy robię właściwe rzeczy. Wszelkie przykłady w tym kierunku pomogą. Jakiej klasy filtrów 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 filtrów potrzebuję dodać trochę wartości 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");
        }
    }
}

Edytuj kod

Zmodyfikowałem mój plik security.xml i plik klasy

<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óry wywołuje każdy adres URL. Czy jest to jakakolwiek alternatywa dla metody filtrowania połączeń tylko raz, gdy użytkownik jest uwierzytelniony?

questionAnswers(2)

yourAnswerToTheQuestion