管理员访客身份登录用户账户,java web/springboot/mybatis实现只能看用户信息但不允许修改
上線的項目,不可避免的會遇見用戶提出的bug,這時候我們可能需要去用戶的頁面重現(xiàn)用戶所說的Bug,為了安全,最好進入用戶頁面的時候不能修改用戶的數(shù)據(jù),只能查看。
對于上述需求實現(xiàn)的方法有多種:
1:根據(jù)ajax請求方式來進行攔截,如果ajax請求方式不是get請求,則在filter或者aop中攔截掉,這種方法實現(xiàn)起來最簡單,能較好的在分布式系統(tǒng)發(fā)揮工作,但是要求接口必須是restful風格。
?
2:在操作dao之前進行攔截,在進行數(shù)據(jù)庫操作之前如果檢測到當前的用戶只允許查看,則拋異常進行攔截,下面講述此種方式:
大致過程:
(1)編寫mybatis攔截器攔截所有的增刪改sql。
(2):若是訪客身份登錄用戶賬戶則分配一個cookie用來表明當前用戶是訪客身份,以便于mybatis攔截器判斷,也可以將此標志放在session中,在mybatis攔截器中通過sessionId從session中判斷用戶是否訪客身份。
(3):在異常處理器中捕獲攔截器攔截后拋出的異常,并返回提示給客戶端。
具體實現(xiàn):
(1)首先編寫mybatis攔截器以便于攔截所有的修改sql,代碼如下:
package cn.lll.iic.ssp.mgr.common;import cn.lll.framework.exception.IllegalUpdateException; import cn.lll.framework.util.ToolUtil; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.*;import java.util.Properties;/*** @time:2020/9/28* @function:攔截sql更新操作*/ @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) }) public class MybatisEventInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {//判斷登錄來源String origin = ToolUtil.getCookieValue("origin");System.out.println(origin);if ("admin".equals(origin)){throw new IllegalUpdateException("當前登錄身份不允許更新");}return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {} }獲取cookie方法對應代碼:
/*** @description:從當前線程上線文獲取請求參數(shù),必須在spring web項目下使用* @param paramName 參數(shù)名* @return java.lang.String*/public static String getCurrentUsername(String paramName){ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = servletRequestAttributes.getRequest();return request.getParameter(paramName);}//從當前請求中獲取指定cookie的值,必須在spring web項目下使用public static String getCookieValue(String key){ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = servletRequestAttributes.getRequest();Cookie[] cookies = request.getCookies();if (cookies == null) return "";for (Cookie cookie : cookies) {if ( Objects.equals(key, cookie.getName()) ){return cookie.getValue();}}return "";}應用啟動加載攔截器:
@Configuration public class SspCfg {@Autowiredprivate List<SqlSessionFactory> sqlSessionFactoryList;//注入的bean執(zhí)行的初始化方法,執(zhí)行一次@PostConstructpublic void addMySqlInterceptor() {MybatisEventInterceptor interceptor = new MybatisEventInterceptor();//攔截sql更新操作for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {sqlSessionFactory.getConfiguration().addInterceptor(interceptor);}} }(2):編寫訪客登錄接口,并設(shè)置cookie,登錄成功后跳轉(zhuǎn)到主頁。
@GetMapping(path = "/login-admin")public JsonResult adminLogin(@RequestParam(name = "username") String username, @RequestParam(name = "pwd") String adminPwd){try {SysUser user = iMgrLoginService.adminLogin(username, adminPwd);this.saveSession(this.SYS_AUTH_SESSION_USERNAME, user);//cookie注明是admin登錄用戶賬戶Cookie flagCookie = new Cookie("origin", "admin");flagCookie.setPath("/");this.response.addCookie(flagCookie);this.response.sendRedirect("/index.html");return null;} catch (BusException | IOException e) {return JsonResult.createFail(e);}}除此之外,在用戶登錄之前我們還需將新加的origin cookie刪除,以免影響用戶登錄:
(3):處理攔截器拋出的自定義異常。
異常代碼:
/*** @time:2020/9/29* @function:不允許的sql更新異常*/ public class IllegalUpdateException extends RuntimeException{public IllegalUpdateException(String message) {super(message);} }結(jié)束。
總結(jié)
以上是生活随笔為你收集整理的管理员访客身份登录用户账户,java web/springboot/mybatis实现只能看用户信息但不允许修改的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mongodb导出查询结果
- 下一篇: Apexchart整数多出小数点