-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
271 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package exception; | ||
|
||
import org.apache.shiro.authc.IncorrectCredentialsException; | ||
import org.apache.shiro.authc.UnknownAccountException; | ||
import org.springframework.web.servlet.HandlerExceptionResolver; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Created by xuweijie on 2017/3/10. | ||
* 统一异常处理类 | ||
* 可以在controller方法、service方法、dao实现类中抛出异常 | ||
* 要求dao、service、controller遇到异常全部向上抛出异常,方法向 上抛出异常throws Exception | ||
*/ | ||
public class CustomExceptionResolver implements HandlerExceptionResolver { | ||
|
||
//前端控制器DispatcherServlet在进行HandlerMapping、 | ||
// 调用HandlerAdapter执行Handler过程中,如果遇到异常就会执行此方法 | ||
//参数中的handler是最终要执行的Handler,它的真实身份是HandlerMethod | ||
//ex就是接受到的异常信息 | ||
|
||
public ModelAndView resolveException(HttpServletRequest httpServletRequest, | ||
HttpServletResponse httpServletResponse, | ||
Object handler, Exception ex) { | ||
|
||
ex.printStackTrace(); | ||
/*统一异常处理代码 | ||
*针对系统自定义的CustomException异常,就可以直接从异常中获取异常信息,将异常处理在错误页面展示 | ||
*异常信息 | ||
* */ | ||
String message=null; | ||
CustomException customException=null; | ||
|
||
//如果ex是系统自定义的异常,我们就直接取出异常信息 | ||
if(ex instanceof CustomException){ | ||
customException= (CustomException) ex; | ||
}else { | ||
customException=new CustomException("未知错误"); | ||
} | ||
|
||
//错误信息 | ||
message=customException.getMessage(); | ||
httpServletRequest.setAttribute("message",message); | ||
|
||
try{ | ||
//转向到错误页面 | ||
httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(httpServletRequest,httpServletResponse); | ||
}catch (ServletException e){ | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return new ModelAndView(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package filter; | ||
|
||
import com.google.code.kaptcha.Constants; | ||
import org.apache.shiro.authc.AuthenticationToken; | ||
import org.apache.shiro.subject.Subject; | ||
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter; | ||
import org.apache.shiro.web.util.WebUtils; | ||
|
||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
|
||
/** | ||
* Created by xuweijie on 2017/3/10. | ||
* shiro的表单过滤器:FormAuthenticationFilter,重写表单过滤器,实现先通过验证码验证再验证账户密码 | ||
*/ | ||
public class CustomFromAuthenticationFilter extends FormAuthenticationFilter{ | ||
|
||
|
||
@Override | ||
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { | ||
HttpServletRequest httpServletRequest= (HttpServletRequest) request; | ||
HttpSession session=httpServletRequest.getSession(); | ||
String verifyCode; | ||
try{ | ||
verifyCode=httpServletRequest.getParameter("verifyCode").toUpperCase(); | ||
}catch (NullPointerException e){ | ||
verifyCode=null; | ||
} | ||
//判断验证码输入是否正确 | ||
if(verifyCode!=null && !verifyCode.equals(session.getAttribute(Constants.KAPTCHA_SESSION_KEY))){ | ||
//如果校验失败,将验证码错误的失败信息,通过shiroLoginFailure设置到request中 | ||
httpServletRequest.setAttribute("shiroLoginFailure","randomCodeError"); | ||
//拒绝访问,不再校验账号和密码 | ||
return true; | ||
} | ||
return super.onAccessDenied(request, response); | ||
} | ||
|
||
@Override | ||
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception { | ||
WebUtils.issueRedirect(request,response,getSuccessUrl()); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package web; | ||
|
||
import entity.User; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.subject.Subject; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
/** | ||
* Created by xuweijie on 2017/3/11. | ||
*/ | ||
@Controller | ||
public class HomepageController { | ||
|
||
@RequestMapping(value = "/homepage") | ||
public String loginsuccess( Model model) throws Exception{ | ||
Subject subject= SecurityUtils.getSubject(); | ||
String username= (String) subject.getPrincipal(); | ||
model.addAttribute("username",username); | ||
return "Homepage"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,45 @@ | ||
package web; | ||
|
||
import com.google.code.kaptcha.Constants; | ||
import entity.User; | ||
import exception.CustomException; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.authc.AuthenticationException; | ||
import org.apache.shiro.authc.UsernamePasswordToken; | ||
import org.apache.shiro.authc.IncorrectCredentialsException; | ||
import org.apache.shiro.authc.UnknownAccountException; | ||
import org.apache.shiro.subject.Subject; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* Created by xuweijie on 2017/3/3. | ||
*/ | ||
@Controller | ||
public class LoginController { | ||
public class LoginController{ | ||
|
||
@RequestMapping(value = "/login",method = RequestMethod.POST) | ||
public String login(HttpServletRequest request, Model model){ | ||
CustomException customException=null; | ||
String verifyCode=request.getParameter("verifyCode").toUpperCase(); | ||
String username=request.getParameter("username"); | ||
String password=request.getParameter("password"); | ||
//判断验证码输入是否正确 | ||
if(verifyCode.equals(request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY))){ | ||
if((username!=null && password!=null)){ | ||
UsernamePasswordToken token=new UsernamePasswordToken(username,password); | ||
Subject subject= SecurityUtils.getSubject(); | ||
try{ | ||
subject.login(token); | ||
}catch (AuthenticationException e){ | ||
customException=new CustomException(e.getMessage()); | ||
} | ||
if( subject.isAuthenticated()){ | ||
subject.logout(); | ||
model.addAttribute("username",username); | ||
return "/loginsuccess"; | ||
}else { | ||
model.addAttribute("exception",customException.getMessage()); | ||
return "/refuse"; | ||
} | ||
@RequestMapping("/login") | ||
public String login(HttpServletRequest request,Model model) throws Exception{ | ||
//如果登录失败从request中获取认证异常信息,shiroLoginFailure就是shiro异常类的全限定名 | ||
String exceptionClassName= (String) request.getAttribute("shiroLoginFailure"); | ||
//根据shiro返回的异常类路径判断,抛出指定异常信息 | ||
if(exceptionClassName!=null){ | ||
if (UnknownAccountException.class.getName().equals(exceptionClassName)) { | ||
//最终会抛给异常处理器 | ||
throw new CustomException("账号不存在"); | ||
} else if (IncorrectCredentialsException.class.getName().equals( | ||
exceptionClassName)) { | ||
throw new CustomException("用户名/密码错误"); | ||
} else if("randomCodeError".equals(exceptionClassName)){ | ||
throw new CustomException("验证码错误"); | ||
}else if(AuthenticationException.class.getName().equals(exceptionClassName)){ | ||
throw new CustomException("认证失败"); | ||
}else{ | ||
throw new Exception();//最终在异常处理器生成未知错误 | ||
} | ||
}else { | ||
System.out.print("验证码输入不正确"); | ||
} | ||
return "login"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.