(स्पष्टीकरण के लिए संपादित) में सत्र स्कोप्ड ऑब्जेक्ट्स सेट करना मेरे पास सत्र विशिष्ट डेटा संग्रहीत करने के लिए POJO (सत्र स्टोरेज) है, जिसे मैं सफल प्रमाणीकरण के बाद पॉप्युलेट करना चाहता हूं। चूंकि मैं "सत्र" के दायरे को सेट कर रहा हूं, इसलिए मैं ऑब्जेक्ट के उसी उदाहरण का उपयोग करने के लिए मेनकंट्रोलर और प्रमाणीकरण SUccesshandler की अपेक्षा कर रहा हूं।प्रमाणीकरण SUccessHandler
जब मैं वेब ऐप चलाता हूं, तो मुख्य नियंत्रक एक उदाहरण (अपेक्षित के रूप में) शुरू करता है, लेकिन जब मैं लॉग इन करता हूं, तो प्रमाणीकरण Successhandler सत्रस्थल ऑब्जेक्ट को स्वचालित रूप से नहीं चलाता है, क्योंकि यह एक NullPointerException फेंकता है।
मैं इसे जो करना चाहता हूं उसे कैसे प्राप्त करूं?
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionStorage implements Serializable{
long id;
public int getId() {
return id;
}
public SessionStorage() {
System.out.println("New Session Storage");
id = System.currentTimeMillis();
}
}
मुख्य नियंत्रक के रूप में निम्नानुसार है:: यहाँ मेरी कोड के कुछ अंश हैं
@Controller
@Scope("request")
@RequestMapping("/")
public class MainController {
@Autowired
private SessionStorage sessionStorage;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
System.out.println(sessionStorage.getId()); //Works fine
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
}
AuthentificationSuccesshandler (जहां त्रुटि फेंक दिया जाता है):
public class AuthentificationSuccessHandler implements AuthenticationSuccessHandler {
@Autowired
private SessionStorage sessionStorage;
@Override
public void onAuthenticationSuccess(HttpServletRequest hsr, HttpServletResponse hsr1, Authentication a) throws IOException, ServletException {
System.out.println("Authentication successful: " + a.getName());
System.out.println(sessionStorage.getId()); //NullPointerException
}
}
के प्रासंगिक भाग spring-security.xml:
<beans:bean id="authentificationFailureHandler" class="service.AuthentificationFailureHandler" />
<beans:bean id="authentificationSuccessHandler" class="service.AuthentificationSuccessHandler" />
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/secure/**" access="hasRole('USER')" />
<form-login
login-page="/login"
default-target-url="/index"
authentication-failure-handler-ref="authentificationFailureHandler"
authentication-failure-url="/login?error"
authentication-success-handler-ref="authentificationSuccessHandler"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>
वेब xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
मुझे पता है यह एक पुराने सवाल है, लेकिन मैं एक ही मुद्दे में चल रहा हूँ और यदि आप एक समाधान के साथ आया था सोच रहा था? – lastmannorth