</pre><pre name="code" class="java">package cn.fulong.sso.action;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import cn.fulong.common.web.action.BaseAction;
import cn.fulong.mis.attendance.service.NewsService;
import cn.fulong.mis.entity.InfNews;
import cn.fulong.mis.entity.SysUser;
import cn.fulong.sso.cookies.CookiesIO;
import cn.fulong.sso.service.LoginService;
public class LoginAction extends BaseAction implements ServletRequestAware,
ServletResponseAware{
private static final long serialVersionUID = 5310965433770356385L;
private String username;
private String pwd;
private String code;
private SysUser sysUser;
private SysUser suser;
private LoginService loginService;
private CookiesIO cookiesIO;
private String msg;
private String pwd1;
private String pwd2;
private String url;
private String flag;
private String info;
private String autolog;
private HttpServletResponse response;
private HttpServletRequest request;
private List<InfNews> newsList;
private NewsService newsService;
public NewsService getNewsService() {
return newsService;
}
@Resource(name = "newsServiceImpl")
public void setNewsService(NewsService newsService) {
this.newsService = newsService;
}
public List<InfNews> getNewsList() {
return newsList;
}
public void setNewsList(List<InfNews> newsList) {
this.newsList = newsList;
}
public LoginService getLoginService() {
return loginService;
}
@Resource(name = "loginServiceImpl")
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public CookiesIO getCookiesIO() {
return cookiesIO;
}
@Resource(name = "cookiesIO")
public void setCookiesIO(CookiesIO cookiesIO) {
this.cookiesIO = cookiesIO;
}
public String getPwd1() {
return pwd1;
}
public void setPwd1(String pwd1) {
this.pwd1 = pwd1;
}
public String getPwd2() {
return pwd2;
}
public void setPwd2(String pwd2) {
this.pwd2 = pwd2;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public SysUser getSysUser() {
return sysUser;
}
public void setSysUser(SysUser sysUser) {
this.sysUser = sysUser;
}
public SysUser getSuser() {
return suser;
}
public void setSuser(SysUser suser) {
this.suser = suser;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getAutolog() {
return autolog;
}
public void setAutolog(String autolog) {
this.autolog = autolog;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
/*public String go() throws Exception {
sysUser = (SysUser) this.getUser();
return SUCCESS;
}*/
public String goChange(){
sysUser = (SysUser) this.getUser();
info=this.getInfo();
return SUCCESS;
}
public String change() throws Exception {
if (StringUtils.isBlank(pwd1)||StringUtils.isBlank(pwd2)){
addActionError("密码不能为空,请重新输入!");
return INPUT;
}
if (!pwd1.equals(pwd2)) {
addActionError("两个密码不相同,请重新输入!");
return INPUT;
}
sysUser = (SysUser) this.getUser();
System.out.println("sysUser="+sysUser);
if (sysUser == null) {
addActionError("请重新登录再试!");
return INPUT;
}
username = sysUser.getUsername();
msg = loginService.changePwd(username, pwd1);
if (!StringUtils.isBlank(msg)) {
addActionError(msg);
return INPUT;
}
String autolog = cookiesIO.readCokie(request, response, "autolog");
//如果有登录标记重写cookie
if(autolog.equals("1")){
cookiesIO.writeCokie(request, response, "username", username, 30);
cookiesIO.writeCokie(request, response, "pwd", pwd1, 30);
cookiesIO.writeCokie(request, response, "autolog", autolog, 30);
}
//cookiesIO.writeCokie(request, response, "autolog", "2", 30);
this.removeUser();
sysUser = loginService.getUserByNamePwd(username, pwd1);
this.saveUser(sysUser);
return SUCCESS;
}
public String login() throws Exception{
newsList = newsService.getNews();
suser = (SysUser) this.getUser();
if(suser==null){//session为空时
if(username!=null && pwd != null){//判断前台用户名和密码不为空时
sysUser = loginService.getUserByNamePwd(username, pwd);
if(sysUser != null){//用户名和密码正确
if("1".equals(sysUser.getFlag())){
this.saveUser(sysUser);
if(autolog.equals("1")){//有自动登录标记
cookiesIO.writeCokie(request, response, "username", username, 30);
cookiesIO.writeCokie(request, response, "pwd", pwd, 30);
cookiesIO.writeCokie(request, response, "autolog", autolog, 30);
String username = cookiesIO.readCokie(request, response, "username");
String pwd = cookiesIO.readCokie(request, response, "pwd");
String autolog = cookiesIO.readCokie(request, response, "autolog");
if(pwd.equals("2")){//判断是否是第一次登录,是第一次登录强制跳转到修改密码页,不是跳转到登录页
flag="1";
this.setInfo("1");
return "success1";
}else{
flag="1";
msg="";
}
}else{//没有自动登录标记
if(pwd.equals("2")){
flag="1";
this.setInfo("1");
return "success1";
}else{
flag="1";
msg="";
}
}
}else{
flag="2";
msg="您的帐号已被停用!";
}
}else{//用户名或密码错误
flag="2";
msg="用户名或密码错误!";
}
}else{//前台用户名和密码为空时
String username = cookiesIO.readCokie(request, response, "username");
String pwd = cookiesIO.readCokie(request, response, "pwd");
String autolog = cookiesIO.readCokie(request, response, "autolog");
if(username!=null && pwd != null && autolog.equals("1")){//cookies不为空
sysUser = loginService.getUserByNamePwd(username, pwd);
if(sysUser != null){//用户名和密码正确
if("1".equals(sysUser.getFlag())){
this.saveUser(sysUser);
cookiesIO.clearCokie(request, response);
cookiesIO.writeCokie(request, response, "username", username, 30);
cookiesIO.writeCokie(request, response, "pwd", pwd, 30);
cookiesIO.writeCokie(request, response, "autolog", autolog, 30);
flag="1";
msg="";
}else{
flag="2";
msg="您的帐号已被停用!";
}
}else{//用户名或密码错误
flag="2";
msg="用户名或密码错误!";
}
}else if(username!=null && pwd != null && autolog.equals("2")){//cookies为空时
flag="2";
msg="2";
}
}
}
if(suser !=null){//session不为空时
sysUser = loginService.getUserByNamePwd(suser.getUsername(), suser.getPassword());
if(sysUser != null){//用户名和密码正确
if("1".equals(sysUser.getFlag())){
this.saveUser(sysUser);
flag="1";
msg="";
}else{
flag="2";
msg="您的帐号已被停用!";
}
}else{//用户名或密码错误
flag="2";
msg="用户名或密码错误!";
}
}
if(msg!="")
return INPUT;
else
return SUCCESS;
}
public String logout() throws Exception {
this.removeUser();
cookiesIO.writeCokie(request, response, "autolog", "2", 30);
//cookiesIO.clearCokie(request, response);
return SUCCESS;
}
public void saveUser(SysUser user) {
getSession().setAttribute("user", user);
}
public void removeUser() {
getSession().setAttribute("user", null);
getSession().invalidate();
}
}
<div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">//读写cookie的工具类</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">package cn.fulong.sso.cookies;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import java.io.IOException;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import javax.servlet.ServletException;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import javax.servlet.http.Cookie;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import javax.servlet.http.HttpServlet;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import javax.servlet.http.HttpServletRequest;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import javax.servlet.http.HttpServletResponse;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import org.springframework.stereotype.Repository;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">@Repository("cookiesIO")</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">public class CookiesIO extends HttpServlet {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> //private static final long serialVersionUID = 67634472886L;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> private HttpServletRequest request;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> private HttpServletResponse response;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> /**</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> * 根据名称读取Cookie</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> *</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> */</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> public String readCokie(HttpServletRequest request, HttpServletResponse response,</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> String name) throws ServletException, IOException {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> String value = null;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> if (name != null) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> Cookie cookies[] = request.getCookies();</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> if (cookies != null && cookies.length >= 2) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> for (int i = 0; i < cookies.length; i++) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> Cookie cookie = cookies[i];</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> if (name.equals(cookie.getName())) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> value = cookie.getValue();</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }else{</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> value="";</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> return value;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> /**</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> * 清空cookie</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> */</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> public void clearCokie(HttpServletRequest request, HttpServletResponse response)</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> throws ServletException, IOException {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> Cookie[] cookies = request.getCookies();</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> for (int i = 0; i < cookies.length; i++) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> Cookie cookie = cookies[i];</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> cookie.setMaxAge(0);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> response.addCookie(cookie);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> /**</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> * 写Cookie</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> */</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> public void writeCokie(HttpServletRequest request, HttpServletResponse response,</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> String name, String value, int days) throws ServletException, IOException {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> int day = 24 * 60 * 60;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> Cookie cookie = new Cookie(name, value);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> cookie.setMaxAge(days * day);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> response.addCookie(cookie);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> public void writeService(String name,String value,int days){</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> try {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> this.writeCokie(request, response, name, value, days);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> } catch (ServletException e) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> // TODO Auto-generated catch block</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> e.printStackTrace();</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> } catch (IOException e) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> // TODO Auto-generated catch block</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> e.printStackTrace();</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> </div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> /*@Override</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> protected void service(HttpServletRequest request, HttpServletResponse response)</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> throws ServletException, IOException {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> String username = readCokie(request, response, "username");</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> System.out.println(username);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> clearCokie(request, response);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> writeCokie(request, response, "username", "username", 30);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }*/</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">}</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">//service层</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">package cn.fulong.sso.service.impl;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import java.util.List;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import org.apache.commons.lang.StringUtils;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import org.springframework.stereotype.Repository;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import cn.fulong.common.service.impl.BaseServiceImpl;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import cn.fulong.mis.entity.SysUser;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">import cn.fulong.sso.service.LoginService;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">@Repository("loginServiceImpl")</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">public class LoginServiceImpl extends BaseServiceImpl implements LoginService {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> @SuppressWarnings("unchecked")</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> public SysUser getUserByNamePwd(String username, String pwd) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> if (StringUtils.isBlank(username) || StringUtils.isBlank(pwd)) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> return null;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> List userList = baseTransaction.find("from SysUser where username=? and password=?", new Object[] { username,</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> pwd });</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> if (userList == null || userList.size() == 0) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> return null;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> } else {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> SysUser user = (SysUser) userList.get(0);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> return user;</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> public String changePwd(String username, String newPwd) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> List userList = baseTransaction.find("from SysUser where username=? ", new Object[] { username });</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> if (userList == null || userList.size() == 0) {</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> return "无法修改此用户。";</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> SysUser user = (SysUser) userList.get(0);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> user.setPassword(newPwd);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> baseTransaction.update(user);</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> return "";</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "> }</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; ">}</div><div style="background-color: inherit; font-family: 微软雅黑; font-size: 14px; "><br style="background-color: inherit;" /></div>