1)。我是Spring技术的新手。所以我开始登录并注销webapp。 2)。我在jsp中创建了登录页面,我还添加了web.xml和spring-servlet.xml。 现在。如果我想让用户的会话无效我应该怎么做以及应该发生什么变化,请帮助我...我发布登录控制器和所有页面。
controller:
@Controller
public class AdminLoginController extends AbstractController
{
static Logger log = Logger.getLogger(AdminLoginController.class.getName());
@RequestMapping(value = "/loginForm", method ={RequestMethod.GET,RequestMethod.POST})
public ModelAndView showForm(ModelMap model)
{
AdminLoginForm loginForm = new AdminLoginForm();
model.put("loginForm", loginForm);
log.info("Inside Controller returning to loginform page....");
return new ModelAndView( GlobalConstants.LOGIN_PAGE);
}
@RequestMapping(value = "/login" ,method ={RequestMethod.POST, RequestMethod.GET})
public ModelAndView processForm(@ModelAttribute("loginForm")AdminLoginForm loginForm, BindingResult result , HttpServletRequest request, HttpServletResponse response, ModelMap model)
{
try{
loginForm = (AdminLoginForm) model.get("loginForm");
String returnPage="";
model=super.execute(model);
if(result.hasErrors()){
return new ModelAndView(GlobalConstants.ERRORPAGE);
}
AdminLoginWorker worker=new AdminLoginWorker();
boolean status=worker.validateUser(loginForm);
if(status)
{
model.addObject("request", request);
HttpSession session=super.getSession(model);
CommonDTOBean dtoBean=(CommonDTOBean)session.getAttribute("dtoBean");
if("Admin".equalsIgnoreCase(loginForm.getUserType())){
dtoBean.setEmp_id(loginForm.getUserName());
dtoBean.setEmpType("Admin");
session.setAttribute("dtoBean", dtoBean);
return new ModelAndView(GlobalConstants.HOME_PAGE);
}else{
dtoBean.setEmp_id(loginForm.getUserName());
dtoBean.setEmpType("Employee");
session.setAttribute("dtoBean", dtoBean);
return new ModelAndView(GlobalConstants.EMP_HOME_PAGE);
}
}
else
{
return new ModelAndView(GlobalConstants.LOGIN_PAGE);
}
}catch(Exception e){
e.printStackTrace();
}
return new ModelAndView(GlobalConstants.LOGIN_PAGE);
}
and spring-servlet.xml is:
<context:component-scan base-package="com.portal.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
</beans>
1.是否有必要为logout创建securityContentxt.xml。 2.上面的控制器类扩展了一些抽象类,它验证会话是否为空。
我已经尝试过从这里得到的解决方案,但无法解决。我已经配置了spring-security.xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http auto-config="true">
<intercept-url pattern="/loginPage" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<logout logout-success-url="/errorPage" logout-url="//errorPage"/>
<session-management invalid-session-url="/home?invalid=true" />
</http>
我在web.xml中添加了以下代码:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
然后我收到了这个错误:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)
任何人都可以说出这个错误吗?
答案 0 :(得分:1)
注销一个简单的链接,如
<a href="/j_spring_security_logout">Logout</a>
应该足以启动注销控制器。
关于你的其他问题,我真的不明白登录部分是否正常工作,你只需要注销,或者你需要整个系统的帮助。一旦登录工作并且您的用户正确地存储在会话中,您就不需要做任何事情,Spring安全管理所有内容。如果您需要其他帮助,请提供更多信息,我将很乐意为您提供帮助!
干杯
答案 1 :(得分:1)
在你的jsp中这是有效的:
<a href="<c:url value="/j_spring_security_logout"/>">Logout</a>
或者如果您需要从其他来源执行此操作,请在java中进行编程
SecurityContextHolder.clearContext();
并删除会话:
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
}