springboot快速给接口添加认证

springboot-auth(github)

基于springboot实现的一个小组件,可以方便地控制接口访问是否需要登录.

安全认证的两个配置因素:怎么保护(如何访问)、谁(哪个/些接口)需要保护,接下来的步骤3和4分别来解决这两个问题

使用方法

1. pom.xml中添加依赖

    
        com.github.julywind
        springboot-auth
        1.0.2
    

2. 为SpringbootApp添加注解 @EnableJSecurity

    @SpringBootApplication
    @EnableJSecurity
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class);
        }
    }

3. 创建用户类(User.java), 并且实现一个认证接口(Auth.java),接口中会有两个方法:

   a. getUser 从request中获取用户

   b. isAuthorized 判断用户是否有指定角色的权限。角色可以为空,验证方法可自行判断,如果不需要不填写role,只判断user是否为空即可,如范例

    public class User {
        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
    @Component
    public class Author implements AuthUser {
        // to getUser from request
        @Override
        public User getUser(HttpServletRequest request) {
            // custom your verify processor
            if(request.getParameter("code")!=null) {
                User user = new User();
                user.setUsername("admin");
                user.setPassword("password");
                return user;
            }
            return null;
        }
    
        // check user permissions
        @Override
        public boolean isAuthorized(String requiredRole, User user) {
            // custom your permission check handler
            System.out.println(requiredRole);
            System.out.println(user);
            return user!=null;
        }
    }

4. 创建一个controller类,注解@Authorized可以添加在类上,也可以添加到方法上。注:此注解只对RequestMapping即接口方法有效。加在类上意味着这个类中所有的RequestMapping方法都需要认证,role如果不需要,可以为空(同3里所讲)。

如类中引入了Authorized注解,但是其中某一两个方法不需要认证, 则可以在这一两个接口方法上添加注解 @SkipAuthorize(v1.0.2添加了此注解的支持)

    @Authorized(role = "myRequiringRoleName")
    @RestController
    public class MyController {
        // you can get authorized user by param annotation CurrentUser
        @GetMapping("/")
        public Object index(@CurrentUser User user){
            return user;
        }

        // will not exec authorize method
        @SkipAuthorize
        @GetMapping("/skipped")
        public Object skipped(){
            return "i am skipped method";
        }
    }

, 如

 

5. 创建一个异常处理类ControllerAdvice ,用于处理认证失败时抛出的AuthenticationFailedException

    @ControllerAdvice
    @RestController
    public class MyControllerAdvice{
        @ExceptionHandler(value = AuthenticationFailedException.class)
        @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
        public String unAuthorized(AuthenticationFailedException exception){
            return exception.toString();
        }
    }

6. 启动app,进行测试:按照上面代码中的逻辑,不带code参数的都不予通过,反之则输出当前用户,所以返回值应该如下

  # curl http://localhost:8080

 com.github.julywind.auth.exception.AuthenticationFailedException

  # curl http://localhost:8080?code=1

 {
    username: "admin",
    password: "password",
 }

 项目地址: https://github.com/julywind/springboot-auth

你可能感兴趣的:(springboot,java,security)