• springaop实现登陆验证


    1.首先配置好springmvc和springaop

    2.先写好登陆方法,通过注解写代理方法

    通过代理获得登陆方法的参数方法名,然后再aop代理方法内进行登陆验证

    贴出代码

     1 package com.huawei.controller;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.ui.Model;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.bind.annotation.RequestMethod;
    10 import org.springframework.web.bind.annotation.RequestParam;
    11 
    12 import com.huawei.entity.User;
    13 
    14 @Controller
    15 public class LoginController {
    16 
    17     private static Set<User> set;
    18 
    19     public Set<User> getSet() {
    20         return set;
    21     }
    22 
    23     public void setSet(Set<User> set) {
    24         this.set = set;
    25     }
    26 
    27     static{
    28         set = new HashSet<User>();
    29         User user1 = new User(1, "tom", "123");
    30         User user2 = new User(2, "sofiya", "123");
    31         User user3 = new User(3, "xuanxuan", "123");
    32         set.add(user1);
    33         set.add(user2);
    34         set.add(user3);
    35 
    36     }
    37     @SuppressWarnings("unused")
    38     @RequestMapping(value={"/login"},method=RequestMethod.POST)
    39     public String login(@RequestParam("username") String username, @RequestParam("password") String password,Model modle) {
    40         String result=null;
    41         for (User user : set) {
    42             if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
    43                 result = "success";
    44                 break;
    45             } else {
    46                 result = "login";
    47             }
    48         }
    49         return result;
    50     }
    51     @RequestMapping("/view")
    52     public String view(){
    53         return "login";
    54     }
    55 }
     1 package com.huawei.aop;
     2 
     3 import org.aspectj.lang.JoinPoint;
     4 import org.aspectj.lang.annotation.Aspect;
     5 import org.aspectj.lang.annotation.Before;
     6 import org.springframework.stereotype.Component;
     7 
     8 @Component
     9 @Aspect
    10 public class LoginAop {
    11 
    12     @Before("execution(* com.huawei.controller.*.*(..))")
    13     public void befores(JoinPoint point){
    14         System.out.println(point);
    15         System.out.println("LoginAop.befores()");
    16             登陆验证方法省略
    17     }
    18 }
    19     
    Enhancer 重生成代理类
    Enhancer enhancer = new Enhancer();  
    enhancer.setSuperclass(UserServiceImpl.class);  
    enhancer.setCallback(new MyMethodInterceptor());  
    UserServiceImpl userService = (UserServiceImpl)enhancer.create();
  • 相关阅读:
    php extends
    php 冒泡排序
    php base64_encode和base64_decode 编码/解码url
    php use
    php命名空间示范
    php 传一个url抓取此页面所有的链接
    pyspark
    【P1330】 封锁阳光大学
    [p1967] 货车运输
    分治的思想
  • 原文地址:https://www.cnblogs.com/javaweb2/p/6242682.html
Copyright © 2020-2023  润新知