要求任何操作都建立在已经登录女生可爱网名的基础上,登录操作除外。。。。
使用spring aop不仅简单,还不会对其他部件中产生影响
以下具体代码实现:
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.requestc物理运动学公式ontextholder;import org.springframework.web.context.request.rvletrequestattributes;import org.springframework.web.rvlet.modelandview; import javax.rvlet.http.httprvletrequest;/** * 登录验证aop */@component@aspectpublic class loginhelper { private static l深有感触的意思ogger 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 { // 获取ssion中的用户信息 httprvletrequest request = ((rvletrequestattributes) requestcontextholder.getrequestattributes()).getrequest(); string urname = (string) request.getssion().getattribute("urname"); if (urname == null) { logger.info("未登录"); return new modelandview("redirect:/login"); } logger.info("urname: " + urname); return joinpoint.proceed(); }}既然要从ssion中获取用户信息,那么肯定要先保存的。可以自登录方法中保存urname
package com.joey.controller;
import com.joey.model.ur;
import com.joey.rvice.urrvice;
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.rvlet.modelandview;
import javax.annotation.resource;
import javax.rvlet.http.httprvletrequest;
@controller
@requestmapping(“/”)
public class indexcontroller {
private static logger logger = logmanager.getlogger(indexcontroller.class.getname());
@resource(name = "urrvice")private urrvice urrvice;@requestmapping(va喷水器lue = {"", "index", "login"}, method = requestmethod.get)public string index() { return "login";}/** * 管理员/普通用户登陆 * * @param urname * @param password * @return */@requestmapping(value = {"login"}, method = requestmethod.post)public modelandview login(httprvletrequest request, string urname, string password) { int id; try { id = urrvice.login(urname, password); } catch (exception e) { e.printstacktrace(); logger.info("not f教育教学案例ound"); return new modelandview("login") .addobject("msg", "try again"); } ur ur = urrvice.lectbyprimarykey(id); request.getssion().tattribute("urname", ur.getname()); // 保存urname到ssion看这里 return new modelandview(ur.getadmin() == 1 ? "admin" : "home") .addobject("id", ur.getid()) .addobject("urname", ur.getname()) .addobject("description", ur.getdescription()) .addobject("isadmin", ur.getadmin() == 1 ? "admin" : "ur");}@requestmapping(value = "home", method = requestmethod.get)public string home() { return "admin";}
}
到此这篇关于springaop实现登录验证的文章就介绍到这了,更多相关springaop登录验证内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 15:36:41,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/4585b19aa8ff271b9298bbdec9ee43f9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:SpringAOP实现登录验证的操作代码.doc
本文 PDF 下载地址:SpringAOP实现登录验证的操作代码.pdf
留言与评论(共有 0 条评论) |