gpt4 book ai didi

spring - 为同一个 REST Api 结合基本身份验证和表单登录

转载 作者:IT老高 更新时间:2023-10-28 13:50:09 25 4
gpt4 key购买 nike

有没有办法为同一个 REST 服务设置基本身份验证和表单登录?我想让登录用户在登录后通过 Web 浏览器和从运行 curl -u username:password hostname.com/api/process 的命令行触发此服务现在我看到了这个帖子:Basic and form based authentication with Spring security Javaconfig但这与我尝试做的略有不同。有没有办法用spring来设置它?我现在拥有的是这样的:

package com.my.company.my.app.security;

import org.slf4j.LoggerFactory;
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 org.springframework.security.provisioning.JdbcUserDetailsManager;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
DataSource dataSource;

private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/js/**", "/css/**")
.permitAll();

http
.authorizeRequests()
.antMatchers("/api/**")
.authenticated()
.and()
.httpBasic();

http
.authorizeRequests()
.antMatchers("/","/index")
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/j_spring_security_check")
.defaultSuccessUrl("/monitor")
.failureUrl("/login?error")
.usernameParameter("j_username")
.passwordParameter("j_password")
.permitAll()
.and()
.logout()
.logoutUrl("/j_spring_security_logout")
.logoutSuccessUrl("/login?logout")
.permitAll()
.and()
.csrf()
.disable();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?")
.authoritiesByUsernameQuery("SELECT username, authority FROM authorities WHERE username=?");
}

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

唯一的问题是,当调用 hostname.com/indexhostname.com/ 时,它不会重定向到我的登录页面,而是弹出窗口询问基本身份验证凭据。

最佳答案

您可以通过使用多个 http 配置轻松实现这一点,如下所示,此代码仅说明多个 http 配置。我假设您非常了解与 Spring Security 相关的其他基本配置,例如 authenticationManger 等。

    @EnableWebSecurity
public class MultiHttpSecurityCustomConfig {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER").and().withUser("admin").password("password")
.roles("USER", "ADMIN");
}

@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/**").authorizeRequests().anyRequest().hasRole("ADMIN").and().httpBasic();
}
}

@Configuration
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin();
}


}
}

请引用spring security官方链接:Multiple HttpSecurity

我还建议您查看 Secure REST Services with Spring Security

如果您遇到任何问题,请随时发表评论!

关于spring - 为同一个 REST Api 结合基本身份验证和表单登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com