MVC框架-mentawai(8)

来源:互联网 发布:gif 软件 编辑:程序博客网 时间:2024/06/16 18:24

认证

用Mentawai,你可以为你的web网站轻易实现认证机制。Mentawai提供了通过BaseLoginAction或通过AuthenticationFilter的实现细节。

创建登录action

public class LoginAction extends BaseLoginAction {    public String execute() throws Exception {        String user = input.getString("username");        String pass = input.getString("password");        if (user == null || user.trim().equals("")) {            return ERROR;        }        if (pass == null || pass.trim().equals("")) {            return ERROR;        }        if (!user.equals("saoj") || !pass.equals("abc123")) {            return ERROR;        }        setSessionObj(user);        return SUCCESS;    }} 

一旦你使用setSessionObjm,登录用户就会被认为是登录状态,直到session失效(当使用reset方法或session超时)。

建立授权过滤器和应用管理器登录 action

@Overridepublic void loadFilters() {    filter(new AuthenticationFilter());    on(LOGIN, redir("/jsp/login.jsp"));}@Overridepublic void loadActions() {    action("/Login", LoginAction.class)    // 没有方法定义,因此假设执行方法是execute()         .on(SUCCESS, redir("/jsp/welcome.jsp"))        .on(ERROR, fwd("/jsp/login.jsp"));}

保护未认证的请求访问action:

当你添加了一个全局的认证过滤器,所有action在执行前都会自动要求认证。但是,某些action不需要认证,例如:任何前台action都有的注册,和登录页等,为了忽略这些认证你可以如下操作:

action("/User", UserAction.class, "add")    .bypassAuthentication()    .on(ERROR, fwd("/jsp/user/add.jsp"))    .on(CREATED, fwd("/jsp/index.jsp"));

保护未认证访问JSP页面

一个请求可以通过Mentawai controller直接访问JSP页,为了拦截一些需要认证的JSP页,你可以使用如下标签 :

<%@ page contentType="text/html; charset=UTF-8"%><%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%><mtw:requiresAuthentication /><html><body><h2>You must be logged to see this!</h2></body></html>  

登出

你可以使用Mentawai提供的LogoutAction登出。

// License goes here...package org.mentawai.action;import org.mentawai.core.BaseAction;import org.mentawai.filter.AuthenticationFree;/** * A simple Logout action that can be used fot user logout. * This action just calls the session <i>reset()</i> method, to clear the session. * * @author Sergio Oliveira */public class LogoutAction extends BaseAction implements AuthenticationFree {    /**     * Implements the actual logout.     * This method simply calls the session <i>reset()</i> method, to clean the session.     * You may override this method if you want to do other operations when the user logs out.     */    protected void logout() {        session.reset();    }    public String execute() throws Exception {        logout();        return SUCCESS;    }    @Override    public boolean bypassAuthentication(String innerAction) {       return true;    }}

你也可以在应用管理器中进行配置:

action("/Logout", LogoutAction.class)   // 没有配置方法,将会执行 execute()方法        .on(SUCESS, redir("/jsp/index.jsp")); 

你不需要显式指出此action的非认证功能,因为LogoutAction实现了 免认证(AuthenticationFree)

登录成功后转发页面

当认证失败时使客户端回退到第一次登录的原始页,你可以这样做:

action("/User", UserAction.class, "edit")    .comeBackAfterLogin()    .on(ERROR, fwd("/jsp/user/edit.jsp"))    .on(SHOW, fwd("/jsp/user/edit.jsp"))    .on(UPDATED, fwd("/jsp/index.jsp"));

如果你不想通过控制器,而是在JSP页面中实现同样的功能,你可以使用requiresAuthentication 标签:

<%@ page contentType="text/html; charset=UTF-8"%><%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%><mtw:requiresAuthentication redir="true" /><html><body><h2>You must be logged to see this!</h2></body></html>