首页 > 解决方案 > 网站的基本身份验证,以保护网站登录和 API 访问

问题描述

我正在研究正在开发的网站的安全模型。在研究了网络之后,我发现有几种安全模型可以保护网站,即 Basic Auth、JWT ...

目前,SSL 尚未启用,因为仍在开发中。网站有一个登录页面并通过 API 进行通信(包括登录和注销)。在登录页面上,作为测试,我尝试使用虚假详细信息登录,然后我查看了开发人员工具以识别安全机制,发现以下截图。我认为该站点正在使用基本身份验证,尽管我注意到电子邮件/密码未编码并且使用自定义登录表单。有人可以确认是否使用了基本身份验证?

开发者工具图片

[请求标题图片][2]

更新:我发现一旦用户通过电子邮件/密码进行身份验证,我应该发布屏幕截图,因为这是返回密钥的地方。在下面的屏幕截图中,投标人令牌和投标人秘密被发送回客户端。我认为这些是通过后端加密生成的。所以我认为它不是 JWT,但这是创建密钥而不是在标头中发送而是在响应正文中发送的合适方法吗?

用户登录后的网络选项卡

登录表格代码:

 {

        /* prepare ui */
        progress.classList.remove('hide');
        login_btn.innerText = 'Logging In';
        login_btn.setAttribute('disabled', true);

        /* make http request */
        var http = new XMLHttpRequest();
        var url = SERVER + '/api/bidder/login';
        var body = {
            email: email.value,
            password: password.value
        };

        http.open('POST', url, true);
        http.setRequestHeader('Content-type', 'application/JSON');

        http.onreadystatechange = function () { //Call a function when the state changes.
            if (http.readyState == 4 && http.status == 200) {
                var res = JSON.parse(http.responseText);

                if (res.status) {

                    localStorage.setItem("bidData", JSON.stringify(res.data));
                    window.location.href = window.location.href.replace('login.html','');

                } else {
                    Toast.show('Danger', res.message);
                }

                /* reset ui */
                progress.classList.add('hide');
                login_btn.innerText = 'Log In';
                login_btn.removeAttribute('disabled');

            }
        }
        http.send(JSON.stringify(body));

    }

标签: node.jsbasic-authentication

解决方案


Spring Security 是 Spring 项目中最基本的身份验证如果要启用 Spring Security,首先必须将 Spring Security 库添加到您的项目中。添加后,您创建一个类来配置 Spring 安全性。

Spring 安全性的类配置中的一个函数。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .cors()
                .and()
            .csrf()
                .disable()
            .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
            .authorizeRequests()
                .antMatchers("/",
                    "/api/statistical/**",
                    "/static/**",
                    "/webjars/**",
                    "/img/**",
                    "/css/**",
                    "/js/**",
                    "/api/diary/**")
                    .permitAll()
                .antMatchers("/api/auth/**")
                    .permitAll()
                .antMatchers("/api/user/checkUsernameAvailability", "/api/user/checkEmailAvailability")
                    .permitAll()
                .antMatchers(HttpMethod.GET, "/api/users/**") //, "/api/polls/**"
                    .permitAll()
                .anyRequest()
                    .authenticated();

推荐阅读