में उपयोगकर्ता अधिकृत करने के बाद कस्टम फ़िल्टर जोड़ने के लिए कैसे मैं वसंत सुरक्षा 3 के लिए नौसिखिया हूँ। मैं उपयोगकर्ताओं के लॉगिन के लिए भूमिका का उपयोग कर रहा हूं।वसंत आवेदन
उपयोगकर्ता के आवेदन में अधिकृत होने के बाद मैं कुछ सत्र मूल्य जोड़ना चाहता हूं। शायद मुझे कुछ फ़िल्टर चाहिए ताकि यह मेरी विधि पर रीडायरेक्ट हो जो कुछ सत्र मान जोड़ता है। मैंने अपनी सुरक्षा.एक्सएमएल फ़ाइल को कॉन्फ़िगर किया है लेकिन मुझे यकीन नहीं है कि मैं सही चीजें कर रहा हूं या नहीं। उस दिशा में कोई भी उदाहरण मदद करेगा। मुझे किस फ़िल्टर क्लास का उपयोग करना चाहिए? मुझे 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"/>
मेरी फ़िल्टर क्लास विधि मुझे कुछ सत्र मान जोड़ने की आवश्यकता है।
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");
}
}
}
संपादित कोड
मैं अपने security.xml फ़ाइल और वर्ग फ़ाइल
<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;
}
}
}
कौन सा हर यूआरएल का आह्वान संशोधित किया है। क्या उपयोगकर्ता फ़िल्टर प्रमाणीकृत होने पर केवल फ़िल्टर विधि को कॉल करने का कोई विकल्प है?
उपर्युक्त कोड में समस्या क्या है? क्या आप लॉगिन सफलता यूआरएल के अंदर सत्र myValue विशेषता नहीं प्राप्त कर रहे हैं? –
@ सुनीलचवन: यह उपयोगकर्ता प्रमाणित होने से पहले इस फ़िल्टर को कॉल करता है। – Raje
आप यह कैसे कह सकते हैं? (एपीआई) देखें [http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/AuthenticationSuccessHandler.html] विवरण। यह स्पष्ट प्रमाणीकरण के बाद स्पष्ट रूप से कहता है कि यह प्रमाणीकरण सफलता विधि –