java - Spring 启动安全

标签 java spring-boot spring-security

我想对此“/realtime/updates”网址进行限制,但如果我请求“/anyurl”,它会将我重定向到登录页面。我只想重定向到“/realtime/updates”的登录页面。

@Configuration
@EnableWebSecurity
   @EnableGlobalMethodSecurity(prePostEnabled = true)
   public class WebSecConfig1 extends WebSecurityConfigurerAdapter {
   @Override
    public void configure(WebSecurity web) throws Exception {
    }
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);

        http.authorizeRequests().antMatchers("/realtime/updates").hasRole("USER").anyRequest().authenticated().and().formLogin();



    }
    @Autowired
    public void ConfigGlobal(AuthenticationManagerBuilder auth) throws Exception{
        auth.inMemoryAuthentication().withUser("abc").password("123").roles("USER");
    }
}

最佳答案

您需要更改 protected void configure 方法中的一个片段。 试试这个:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
        .authorizeRequests()
            .antMatchers("/**").permitAll()
            .antMatchers("/realtime/updates").hasRole("USER")
        .and().formLogin().permitAll();
}

关于java - Spring 启动安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45611648/

相关文章:

java - 计算类的实例

java - 具有有效证书的 spring boot https 获取 ERR_SSL_VERSION_OR_CIPHER_MISMATCH,自签名工作正常

spring-boot - 带有 Bucket4j 的 Spring Boot 2 以防止 DDOS 攻击

maven - Spring Boot GS : ComponentScan and ClassNotFoundException for ConnectionFactory

jquery - CORS 问题 - 请求的资源上不存在 'Access-Control-Allow-Origin' header

java - 如何在 Spring 中注销 oauth2 客户端?

rest - 如何在 Grails 中使用 spring security rest 插件进行身份验证

java - Netty TLS 异常 PEER_DID_NOT_RETURN_A_CERTIFICATE

java - shell脚本在手动执行时运行,但在crontab中执行到一半

java - 以 O(log n) 复杂度计算一个值在排序数组中出现的次数