BOS系统开发笔记(3)----登陆模块开发以及权限校验

一、登陆模块

1、UserAction

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {
	//属性驱动,接收页面输入的验证码
	private String checkcode;
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}
	
	@Autowired
	private IUserService userService;
	
	/**
	 * 用户登录
	 */
	public String login(){
		//从Session中获取生成的验证码
		String validatecode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
		//校验验证码是否输入正确
		if(StringUtils.isNotBlank(checkcode) && checkcode.equals(validatecode)){
			//输入的验证码正确
			User user = userService.login(model);
			if(user != null){
				//登录成功,将user对象放入session,跳转到首页
				ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
				return HOME;
			}else{
				//登录失败,,设置提示信息,跳转到登录页面
				//输入的验证码错误,设置提示信息,跳转到登录页面
				this.addActionError("用户名或者密码输入错误!");
				return LOGIN;
			}
		}else{
			//输入的验证码错误,设置提示信息,跳转到登录页面
			this.addActionError("输入的验证码错误!");
			return LOGIN;
		}
	}
}


2、UserService接口以及实现类UserServiceImp

@Service
@Transactional
public class UserServiceImpl implements IUserService{
	@Autowired
	private IUserDao userDao;
	/***
	 * 用户登录
	 */
	public User login(User user) {
		//使用MD5加密密码
		String password = MD5Utils.md5(user.getPassword());
		return userDao.findUserByUsernameAndPassword(user.getUsername(),password);
	}
}


3、UserDao接口及其实现类UserDaoImp

@Service
@Transactional
public class UserServiceImpl implements IUserService{
	@Autowired
	private IUserDao userDao;
	/***
	 * 用户登录
	 */
	public User login(User user) {
		//使用MD5加密密码
		String password = MD5Utils.md5(user.getPassword());
		return userDao.findUserByUsernameAndPassword(user.getUsername(),password);
	}
}

4、struts.xml的配置

!-- 用户管理 -->
		<action name="userAction_*" class="userAction" method="{1}">
			<result name="login">/login.jsp</result>
			<result name="home">/index.jsp</result>
		</action>

5、用户注销功能,将session销毁并跳转到登陆页面

public String logout(){
 ActcinoContext.getContext().getSession().invalidate();
return "login";
}

二、权限校验

package com.iteason.bos.web.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class MyInterceptor extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		
		//如果没有登陆,就让其登陆
		Object object = ActionContext.getContext().getSession().get("loginUser");
		if(object != null){
			//放行
			return invocation.invoke();
		}else{
			return "toLogin";
		}
		
	}

	 
}
<!-- 自定义拦截器 -->
	<interceptors>
	<!-- 注册自定义的拦截器 -->
		<interceptor name="myInt" class="com.iteason.bos.web.interceptor.MyInterceptor"></interceptor>
		<!-- 注册自定义拦截器栈 -->
		<interceptor-stack name="myStack">
			<interceptor-ref name="myInt">
				<!-- 配置不拦截的方法 -->
				<param name="excludeMethods">login</param>
			</interceptor-ref>
				<!-- 默认拦截器栈 -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</interceptor-stack>
	</interceptors>
	
	<!-- 指定默认拦截器栈 -->
	<default-interceptor-ref name="myStack"></default-interceptor-ref>
	
	
	
	
	<!-- 全局结果集 -->
	<global-results>
		<result name="toLogin">/login.jsp</result>
	</global-results>


猜你喜欢

转载自blog.csdn.net/pbrlovejava/article/details/80963723