javaweb session 사용자 로그 인 완료
4256 단어 Java(기능 편)
package com.****;
import com.wlsq.kso.entity.AccountUser;
import com.wlsq.kso.entity.Developer;
import com.wlsq.kso.service.AccountUserService;
import com.wlsq.kso.service.IDeveloperService;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
/**
* Controller
*
* @author zzg
* @date 2017-02-27
*/
@Controller
@RequestMapping(value ="login")
public class LoginController
{
@Autowired
private IDeveloperService developerService;
@Autowired
private AccountUserService accountUserService;
// 。
@RequestMapping(value ="/logout.html")
public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "text/html;charset=UTF-8");
HttpSession session = request.getSession(false);
if (session == null) {
// ,
String url = response.encodeRedirectURL(request.getContextPath()
+ "/login.jsp");
response.sendRedirect(url);
System.out.println(" 1:"+url);
return;
}
// session
session.removeAttribute("user");
// ,URL
String url = response.encodeRedirectURL(request.getContextPath()
+ "/login.jsp");
response.sendRedirect(url);
System.out.println(" 2:"+url);
}
// 。
@RequestMapping({"/accountUserLogin.html"})
public ModelAndView accountUserLogin(HttpServletRequest request, HttpServletResponse response,@RequestParam String username, @RequestParam String password)
{
ModelAndView modelAndView = new ModelAndView();
HttpSession session = request.getSession();
// -- 。
// String reallyCode = (String) session.getAttribute("code");
// if (!code.equalsIgnoreCase(reallyCode))
// {
// modelAndView.addObject("error", " ");
// modelAndView.setViewName("redirect:/login/accountUserLogin.html");
// } else {
Map map = new HashMap();
map.put("username", username);
map.put("password", password);
AccountUser acountUser = this.accountUserService.selectAccountUserByUsernamePassword(map);
if (acountUser != null) {
// session 30
String sessionId = session.getId();
Cookie cookie = new Cookie("JSESSIONID", sessionId);
cookie.setMaxAge(60 * 30);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
// ,key String value (model)!!
session.setAttribute("user", acountUser);
//
// if (developer.getUserType().intValue() == 0)
// {
// modelAndView.setViewName("front_end/application/applications");
// }
// else {
// modelAndView.setViewName("front_end/application/applications");
// }
acountUser.setUpdatedate(new Date());
this.accountUserService.updateByPrimaryKeySelective(acountUser);
modelAndView.setViewName("index");
}
else {
modelAndView.addObject("error", " ");
modelAndView.setViewName("redirect:login/accountUserLogin.html");
}
//}
return modelAndView;
}
}