使用Springboot在https url中的用户名和密码

高度

我需要使用这样的URL访问我的spring boot应用程序:https:// username:password @ IpServer /

我已经实现了https协议,但是这里的问题是如何添加用户并通过url传递

通常,我使用以下URL访问我的应用程序:https:// @ IpServer /

我需要面对这个话题的任何人的帮助

高度

我终于注意到,Spring boot集成了这个apache选项,在您像这样修改应用程序属性后,还应该添加用于Spring安全性的Security依赖项(也适用于https协议):

# Define a custom port instead of the default 8080
server.port=443

#Define http server
#http.port=8080

# Tell Spring Security (if used) to require requests over HTTPS
security.require-ssl=true

# The format used for the keystore 
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=classpath:keystore.p12
# The password used to generate the certificate
server.ssl.key-store-password=keycertifpass
# The alias mapped to the certificate
server.ssl.key-alias=tomcat

之后,在新类中添加基本身份验证,这种身份验证负责添加用户并通过URL传递:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


import com.project.exception.CustomAuthenticationEntryPoint;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private CustomAuthenticationEntryPoint unauthorizedHandler;

    /** Use Basic Authentication **/

    @Override
    public void configure(HttpSecurity http) throws Exception
    {
        http.httpBasic().realmName("user").and().authorizeRequests()
        .antMatchers("/**").hasRole("user")
        .and()
        .csrf().disable()
        .exceptionHandling().authenticationEntryPoint(unauthorizedHandler); 
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception
    {
        auth.inMemoryAuthentication()
        .withUser("user").password(passwordEncoder().encode("pass")).roles("user");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}

对我来说,我已为authenticationEntryPoint自定义了错误处理,可以根据需要添加它,也可以不进行自定义,就可以自由使用。

我希望这个答案能帮助遇到相同问题的人们。

感谢大家。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

HTTPS URL中的用户名和密码是否安全?

使用pkgcloud在OpenStack中验证用户名和密码

为什么wget第一次不使用URL中的用户名和密码

如何使用node.js中的基本身份验证从URL获取用户名和密码?

从3.8+中的URL拆分用户名和密码(不建议使用splituser,别无选择)

使用python向用户询问用户名和密码

Kafka-如何在springboot yml文件中定义用户名和密码?

使用Svn套件的Svn身份验证用户名,密码和URL

使用脚本中的用户名和密码使用GNU屏幕登录

无法使用VBA在网页中输入用户名和密码

如何使用用户名和密码连接Java中的MongoDB 3.2?

如何使用符号在android中验证用户名和密码?

在Android中仅使用用户名和密码通过Firestore进行身份验证

使用Azure用户名和密码在Azure中获取访问令牌

使用用户名和密码在PHP中访问sql

在 Python 代码中安全地使用用户名和密码

如何使用用户名和密码访问 Asp.Net Core 中的 MongoDb?

如何使用CCDT文件,用户名和密码在Python中连接到IBM MQ

IOException当下载使用Java中的用户名和密码保护的文件时

使用php的数据库中的用户名和密码

如何使用类名在Facebook中输入用户名和密码

如何使用自动用户名和密码保存在 html 中?

尝试使用 JavaScript 在 cookie 中存储用户名和密码

在python中使用selenium在输入标签中填写用户名和密码

使用Azure Key Vault存储用户名和密码

如何使用用户名和密码连接LDAP?

使用并行数组的用户名和密码

使用用户名和密码进行密钥验证

如何使用Mercurial保存用户名和密码?