欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot 之 实现登录功能及登录拦截器

程序员文章站 2022-04-15 21:56:30
增加登录退出控制器: 增加登录拦截器: 将登录拦截器配置到容器中: ......

增加登录退出控制器:

# src/main/java/com/wu/controller/logincontroller.java

@controller
public class logincontroller {
    @requestmapping("/user/login")
    public string login(
        @requestparam("username") string username,
        @requestparam("password") string password,
        model model,
        httpsession session) {
        if (!stringutils.isempty(username) && "123456".equals(password)) {
            session.setattribute("loginuser", username);
            return "redirect:/main.html";
        } else {
            model.addattribute("msg", "用户名或密码错误!");
            return "index";
        }
    }

    @requestmapping("/user/logout")
    public string logout(httpsession session) {
        session.invalidate();
        return "redirect:/index.html";
    }
}

增加登录拦截器:

# src/main/java/com/wu/interceptor/loginhandlerinterceptor .java

public class loginhandlerinterceptor implements handlerinterceptor {
    @override
    public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {
        object user = request.getsession().getattribute("loginuser");
        if (user == null) {
            request.setattribute("msg", "无权限请先登录");
            request.getrequestdispatcher("/index.html").forward(request, response);
            return false;
        } else {
            return true;
        }
    }

    @override
    public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception {

    }

    @override
    public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception {

    }
}

将登录拦截器配置到容器中:

# src/main/java/com/wu/config/mymvcconfig.java

@configuration
public class mymvcconfig implements webmvcconfigurer {
    // 将登录拦截器配置到容器中
    @override
    public void addinterceptors(interceptorregistry registry) {
        registry.addinterceptor(new loginhandlerinterceptor())
                .addpathpatterns("/**")
                .excludepathpatterns("/", "/index.html", "/user/login", "/css/**", "/js/**", "/img/**");
    }

    // 配置视图跳转
    @override
    public void addviewcontrollers(viewcontrollerregistry registry) {
        registry.addviewcontroller("/").setviewname("index");
        registry.addviewcontroller("/index.html").setviewname("index");
        registry.addviewcontroller("/main.html").setviewname("dashboard");
    }

    // 将本地化解析器配置到容器中
    @bean
    public localeresolver localeresolver() {
        return new mylocaleresolver();
    }

    // 将视图解析器配置到容器中
    @bean
    public viewresolver myviewresolver() {
        return new myviewresolver();
    }
    
    // 自定义一个视图解析器
    public static class myviewresolver implements viewresolver {
        @override
        public view resolveviewname(string viewname, locale locale) throws exception {
            return null;
        }
    }
}