基于session和token的身份认证方案
2018-06-18 01:21:03来源:未知 阅读 ()
一、基于session的身份认证方案
1.方案图示
2.比较通用的鉴权流程实现如下:
@Component public class AuthInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Map<String, String[]> map = request.getParameterMap(); map.forEach((k,v) -> { if (k.equals("errorMsg") || k.equals("successMsg") || k.equals("target")) { request.setAttribute(k, Joiner.on(",").join(v)); } }); String reqUri = request.getRequestURI(); if (reqUri.startsWith("/static") || reqUri.startsWith("/error") ) { return true; } HttpSession session = request.getSession(true);//参数为true,没有session则创建新的 User user = (User)session.getAttribute(CommonConstants.USER_ATTRIBUTE); if (user != null) { UserContext.setUser(user); } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { UserContext.remove(); } }
@Component public class AuthActionInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { User user = UserContext.getUser(); if (user == null) { String msg = URLEncoder.encode("请先登录","utf-8"); String target = URLEncoder.encode(request.getRequestURL().toString(),"utf-8"); if ("GET".equalsIgnoreCase(request.getMethod())) { response.sendRedirect("/accounts/signin?errorMsg=" + msg + "&target="+target); return false;//修复bug,未登录要返回false }else { response.sendRedirect("/accounts/signin?errorMsg="+msg); return false;//修复bug,未登录要返回false } } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
@Configuration public class WebMvcConf extends WebMvcConfigurerAdapter { @Autowired private AuthActionInterceptor authActionInterceptor; @Autowired private AuthInterceptor authInterceptor; @Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns("/static"); registry .addInterceptor(authActionInterceptor).addPathPatterns("/house/toAdd") .addPathPatterns("/accounts/profile").addPathPatterns("/accounts/profileSubmit") .addPathPatterns("/house/bookmarked").addPathPatterns("/house/del") .addPathPatterns("/house/ownlist").addPathPatterns("/house/add") .addPathPatterns("/house/toAdd").addPathPatterns("/agency/agentMsg") .addPathPatterns("/comment/leaveComment").addPathPatterns("/comment/leaveBlogComment"); super.addInterceptors(registry); } }
二、基于token的身份认证方案
优点:
Token,指的是 OAuth Token 或类似的机制的话,提供的是 认证 和 授权 ,认证是针对用户,授权是针对 App 。其目的是让 某App 有权利访问 某用户 的信息。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 聊聊 OAuth 2.0 的 Token 续期处理 2020-06-08
- Spring Security OAuth 格式化 token 输出 2020-06-08
- 聊聊 OAuth 2.0 的 token expire_in 使用 2020-06-08
- 基于数据库的代码自动生成工具,生成JavaBean、生成数据库文 2020-05-31
- 数据分析 | 基于智能标签,精准管理数据 2020-05-30
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash