首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot - spring安全授权问题

Spring Boot - spring安全授权问题
EN

Stack Overflow用户
提问于 2018-09-20 13:32:13
回答 1查看 59关注 0票数 0

我已经用基本身份验证来保护我的spring引导应用程序。下面是我的春季安全配置。

代码语言:javascript
复制
package com.exxonmobil.asr.backoffice.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
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.web.AuthenticationEntryPoint;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

    private static final String SPRING_SECURITY_PASSWORD = "spring.security.password";
    private static final String SPRING_SECURITY_USERNAME = "spring.security.username";
    @Autowired
    private AuthenticationEntryPoint authEntryPoint;

    @Autowired
    private Environment env;

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser(env.getProperty(SPRING_SECURITY_USERNAME))
                .password(bcryptPasswordEncoder().encode(env.getProperty(SPRING_SECURITY_PASSWORD))).roles("USER");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable().authorizeRequests().antMatchers("/**").hasAnyRole("USER").anyRequest().authenticated().and().httpBasic()
                .authenticationEntryPoint(authEntryPoint);
    }

}

当我试图通过postman访问应用程序rest URL时,我会遇到一些问题。当我在“邮递员”中没有授予任何授权/错误的授权凭证时,我无法访问该资源并获得无效的凭据错误消息。但是,问题是,当我第一次能够使用正确的凭据成功地访问资源时,现在我将凭据更改为无效的凭据并尝试访问资源时,我仍然能够访问它。

任何我都能阻止的方法。

提前谢谢。

你好,法尔汉

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-20 13:46:05

Spring Security创建一个会话,并将Postman保存的cookie发回,然后登录,直到此会话无效。

您可以使用以下命令禁用会话:

代码语言:javascript
复制
http
    .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

在SpringSecurityConfiguration的配置方法中。

但是请记住,您禁用了整个会话管理。如果您只有一个REST,可能会很好,但是如果您还有其他web组件,您可能需要HTTP会话。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52426341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档