栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【springboot】配置登陆校验

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【springboot】配置登陆校验

登录校验
  • 添加配置
#properties
auth.enable=true
auth.user-name=admin
auth.password=admin

# yml
auth:
  enable: true
  user-name: admin
  auth.password: admin
  • 添加配置类
@Component
@ConfigurationProperties(prefix = "auth", ignoreUnknownFields = true)
public class AuthProperties {
    private Boolean enable = true;
    private String userName;
    private String password;

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    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;
    }
}
  • 添加校验配置
package com.xiaoai.config.tomcatAuth;

import org.apache.catalina.CredentialHandler;
import org.apache.catalina.authenticator.Authenticatorbase;
import org.apache.catalina.authenticator.BasicAuthenticator;
import org.apache.catalina.realm.GenericPrincipal;
import org.apache.catalina.realm.MessageDigestCredentialHandler;
import org.apache.catalina.realm.Realmbase;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.security.Principal;
import java.util.Collections;

@Component
@ConditionalOnProperty(prefix = "auth", name = "enable", matchIfMissing = true)
public class AuthTomcatConfig implements WebServerFactoryCustomizer, Ordered {

    @Autowired
    private AuthProperties authProperties;

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        TomcatServletWebServerFactory tomcatServletWebServerFactory = (TomcatServletWebServerFactory) factory;
        tomcatServletWebServerFactory.addContextCustomizers(context -> {

            Realmbase realm = new Realmbase() {
                @Override
                protected String getPassword(String username) {
                    if (authProperties.getUserName().equals(username)) {
                        return authProperties.getPassword();
                    }
                    return null;
                }
                @Override
                protected Principal getPrincipal(String username) {
                    return new GenericPrincipal(username, authProperties.getPassword(), Collections.singletonList("**"));
                }
            };

            CredentialHandler credentialHandler = new MessageDigestCredentialHandler();
            realm.setCredentialHandler(credentialHandler);
            context.setRealm(realm);
            Authenticatorbase digestAuthenticator = new BasicAuthenticator();
            // 设置安全校验
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setAuthConstraint(true); // 是否开启
            securityConstraint.addAuthRole("**");
            // 校验条件收集
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/auth/*");  // 校验路径
            securityConstraint.addCollection(collection);

            context.addConstraint(securityConstraint);
            context.getPipeline().addValve(digestAuthenticator);
        });
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号