SpringAOP實(shí)現(xiàn)登錄驗(yàn)證的操作代碼
要求任何操作都建立在已經(jīng)登錄的基礎(chǔ)上,登錄操作除外。。。。
使用Spring AOP不僅簡單,還不會對其他部件中產(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成支付寶支付的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成支付寶支付的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06IDEA遇到Internal error. Please refer to http://jb. gg/ide/crit
這篇文章主要介紹了IDEA遇到Internal error. Please refer to http://jb. gg/ide/critical-startup-errors的問題及解決辦法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-08-08Java8中對于LocalDateTime的序列化和反序列化問題
這篇文章主要介紹了Java8中對于LocalDateTime的序列化和反序列化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06