SpringAOP實(shí)現(xiàn)登錄驗(yàn)證的操作代碼
要求任何操作都建立在已經(jīng)登錄的基礎(chǔ)上,登錄操作除外。。。。
使用Spring AOP不僅簡(jiǎn)單,還不會(huì)對(duì)其他部件中產(chǎn)生影響
以下具體代碼實(shí)現(xiàn):
package com.joey.util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
/**
* 登錄驗(yàn)證AOP
*/
@Component
@Aspect
public class LoginHelper {
private static Logger logger = LogManager.getLogger(LoginHelper.class.getName());
@Pointcut("within(com.joey.controller..*)&&!within(com.joey.controller.IndexController)") // IndexController中寫了登錄方法
public void login() {
}
@Around("login()")
public Object auth(ProceedingJoinPoint joinPoint) throws Throwable {
// 獲取session中的用戶信息
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String username = (String) request.getSession().getAttribute("username");
if (username == null) {
logger.info("未登錄");
return new ModelAndView("redirect:/login");
}
logger.info("username: " + username);
return joinPoint.proceed();
}
}
既然要從session中獲取用戶信息,那么肯定要先保存的??梢宰缘卿浄椒ㄖ斜4鎢sername
package com.joey.controller;
import com.joey.model.User;
import com.joey.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/")
public class IndexController {
private static Logger logger = LogManager.getLogger(IndexController.class.getName());
@Resource(name = "userService")
private UserService userService;
@RequestMapping(value = {"", "index", "login"}, method = RequestMethod.GET)
public String index() {
return "login";
}
/**
* 管理員/普通用戶登陸
*
* @param username
* @param password
* @return
*/
@RequestMapping(value = {"login"}, method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, String username, String password) {
int id;
try {
id = userService.login(username, password);
} catch (Exception e) {
e.printStackTrace();
logger.info("not found");
return new ModelAndView("login")
.addObject("msg", "Try Again");
}
User user = userService.selectByPrimaryKey(id);
request.getSession().setAttribute("username", user.getName()); // 保存username到session看這里
return new ModelAndView(user.getAdmin() == 1 ? "admin" : "home")
.addObject("id", user.getId())
.addObject("username", user.getName())
.addObject("description", user.getDescription())
.addObject("isAdmin", user.getAdmin() == 1 ? "admin" : "user");
}
@RequestMapping(value = "home", method = RequestMethod.GET)
public String home() {
return "admin";
}}
到此這篇關(guān)于SpringAOP實(shí)現(xiàn)登錄驗(yàn)證的文章就介紹到這了,更多相關(guān)SpringAOP登錄驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot Shiro在Web應(yīng)用中的作用詳解
這篇文章主要為大家介紹了Spring Boot Shiro在Web應(yīng)用中的作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
如何使用SpEL表達(dá)式實(shí)現(xiàn)動(dòng)態(tài)分表查詢
這篇文章主要介紹了如何使用SpEL表達(dá)式實(shí)現(xiàn)動(dòng)態(tài)分表查詢,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java斷點(diǎn)續(xù)傳功能實(shí)例(java獲取遠(yuǎn)程文件)
本文介紹了一種利用 Java 來實(shí)現(xiàn)斷點(diǎn)續(xù)傳的方法。2013-12-12
ByteArrayOutputStream與InputStream互相轉(zhuǎn)換方式
這篇文章主要介紹了ByteArrayOutputStream與InputStream互相轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring Boot中定時(shí)任務(wù)Cron表達(dá)式的終極指南最佳實(shí)踐記錄
本文詳細(xì)介紹了SpringBoot中定時(shí)任務(wù)的實(shí)現(xiàn)方法,特別是Cron表達(dá)式的使用技巧和高級(jí)用法,從基礎(chǔ)語法到復(fù)雜場(chǎng)景,從快速啟用到調(diào)試驗(yàn)證,再到常見問題的解決,涵蓋了定時(shí)任務(wù)開發(fā)的全過程,感興趣的朋友一起看看吧2025-03-03
Springboot 如何設(shè)置啟動(dòng)內(nèi)存
這篇文章主要介紹了Springboot 如何設(shè)置啟動(dòng)內(nèi)存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
玩轉(zhuǎn)SpringBoot中的那些連接池(小結(jié))
這篇文章主要介紹了玩轉(zhuǎn)SpringBoot中的那些連接池(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
java IO流 之 輸入流 InputString()的使用
這篇文章主要介紹了java IO流 之 輸入流 InputString()的使用,以及讀取數(shù)據(jù)的三種方式詳解,非常不錯(cuò),需要的朋友可以參考下2016-12-12
利用Java工具類Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能
這篇文章主要介紹了利用Java工具類Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能,利用Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn),校驗(yàn)的Servlet與今天的第一篇是一樣的,唯一就是驗(yàn)證碼的生成是不一樣的,利用Hutool生成驗(yàn)證碼更快捷.需要的朋友可以參考下2022-10-10

