java - 配置 Http 安全

标签 java spring security https

我有这个网址,我已赋予角色 USER 但我无法访问,并且当前经过身份验证的主体是用户

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().
                antMatchers(PUBLIC_MATCHERS).permitAll().
                antMatchers("/bookDetail/**").hasRole("USER").
                antMatchers("/listOfCreditCards/**").hasRole("USER").
                antMatchers("/shoppingCart/addItem/**").hasRole("USER").
                and().formLogin();

        http
                .csrf().disable().cors().disable()
                .formLogin().failureUrl("/login?error")
                .defaultSuccessUrl("/")
                .loginPage("/login").permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/?logout").deleteCookies("remember-me").permitAll()
                .and()
                .rememberMe();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        GrantedAuthority authority = new SimpleGrantedAuthority("USER");
        UserDetails userDetails = (UserDetails) new User("V", "A", Arrays.asList(authority));
        return new InMemoryUserDetailsManager(Arrays.asList(userDetails));
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
    }

    @Configuration
    protected static class AuthenticationConfiguration extends
            GlobalAuthenticationConfigurerAdapter {

        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("V").password("A").roles("USER");

我得到此输出 -status":403,"error":"Forbidden","message":"访问被拒绝" 关于我应该检查什么并且没有堆栈跟踪的任何建议

最佳答案

代码中唯一授权任何页面的行是:

antMatchers(PUBLIC_MATCHERS).permitAll()

如果这不是您的登录页面,您将无法访问它,因为您尚未授予其权限。您可能需要以下内容:

http.authorizeRequests().
 antMatchers(PUBLIC_MATCHERS).permitAll(). 
 antMatchers("/bookDetail/**").hasRole("USER").
 antMatchers("/listOfCreditCards/**").hasRole("USER").
 antMatchers("/shoppingCart/addItem/**").hasRole("USER").
.and()
.formLogin().loginPage("/loginPage").permitAll()
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/home")
.failureUrl("/loginPage?error")
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/loginPage?logout")
.and()
.csrf()
.and()
.exceptionHandling()
.accessDeniedPage("/accessDenied");

关于java - 配置 Http 安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48133255/

相关文章:

java - 如何将实体及其关系存储在一起?

Spring bean 初始化 - Clojure

java - 从 GCMIntentService 调用 AsyncTask 时应用程序崩溃

java - 系统异常或应用异常

java - REST API 的问题

ios - 当应用程序未激活时如何实现隐私屏幕?

security - 为什么Kubernetes允许在kubelet目录中进行全局读/写?

java - 如果不会重新分配,我是否应该将所有形式参数设置为final?

java - 编译类加载器很有趣,在这种情况下,子类如何从不同的类加载器加载有什么想法吗?

php - 用户提交的链接 - 使用 PHP、MySQL 存储