有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Spring Security成功登录后,java服务器连接丢失

使用Spring security成功登录后,我将断开连接,我的客户端正在尝试重新连接。这是我的配置文件:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery(
                        "select nickname,password, true from client where nickname=?")
                .authoritiesByUsernameQuery(
                        "select username, role from user_roles where username=?");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login")
                .permitAll()
                .antMatchers("/*")
                .access("hasRole('ROLE_USER')");
        http.formLogin()
                .defaultSuccessUrl("/", true);
    }

    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }
}

我用的是瓦丁流。成功登录后,我的主页将显示出来,但在那之后,它将失去连接,并开始以无休止的循环重新连接

这是我的根页面的标题

@Route(value = "")
public class BasePageView extends VerticalLayout

谢谢你的帮助


共 (2) 个答案

  1. # 1 楼答案

    我认为你应该重写超类方法

    protected void configure(AuthenticationManagerBuilder auth) throws Exception

    而不是public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception

    例如,这个示例适用于我:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {    
     auth.inMemoryAuthentication().withUser("user").password("test").roles("USER");
        }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login")
                .permitAll()
                .antMatchers("/*")
                .access("hasRole('ROLE_USER')");
        http.formLogin()
                .defaultSuccessUrl("/", true);
    }
    }
    

    还要仔细检查数据库中的角色名是否正确,我认为应该将antMatchers设置为/**而不是/*来处理多级url

  2. # 2 楼答案

    将这个http.csrf().disable()添加到SecurityConfig中的configure方法中

    我不知道没有它为什么不起作用。是否为Vaadin流量专用。也许其他人会帮助我们理解