【问题标题】:spring-boot restricted urlspring-boot 受限网址
【发布时间】:2014-07-29 11:43:31
【问题描述】:

美好的一天, 我有一个 spring-boot 1.1.4.RELEASE 应用程序,它使用包含作为依赖项的 spring-security,例如:

compile("org.springframework.boot:spring-boot-starter-security")    
compile("org.springframework.security:spring-security-web:4.0.0.M1")
compile("org.springframework.security:spring-security-config:4.0.0.M1")
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE')

我有两种类型的角色:“用户”和“管理员”。后者拥有前者的一切,但也可以访问管理屏幕。在我的 Thymeleaf 页面中,我只将该链接显示给具有管理员角色的用户,效果很好:

<li sec:authorize="hasRole('ADMIN')">
    <i class="fa fa-link"></i><a th:href="@{/admin}">
            Administer User</a>
</li>

但是,如果我手动输入该页面的 url (http://localhost:9001/admin),所有角色都可以访问它。我以为我是通过安全配置类来控制它的:

@Configuration
@EnableWebMvcSecurity
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers( "/" ).permitAll()
                .antMatchers("/admin/").hasRole("ADMIN")  <== also tried .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers( "/resources/**" ).permitAll()
                .antMatchers( "/css/**" ).permitAll()
                .antMatchers( "/libs/**" ).permitAll();

        http
                .formLogin().failureUrl( "/login?error" )
                .defaultSuccessUrl( "/" )
                .loginPage( "/login" )
                .permitAll()
                .and()
                .logout().logoutRequestMatcher( new AntPathRequestMatcher( "/logout" ) ).logoutSuccessUrl( "/" )
                .permitAll();

        http
                .sessionManagement()
                .maximumSessions( 1 )
                .expiredUrl( "/login?expired" )
                .maxSessionsPreventsLogin( true )
                .and()
                .sessionCreationPolicy( SessionCreationPolicy.IF_REQUIRED )
                .invalidSessionUrl( "/" );

        http
                .authorizeRequests().anyRequest().authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        auth.userDetailsService( customUserDetailsService ).passwordEncoder( encoder );
    }

}

我的配置中是否缺少某些内容或不正确?

更新: 根据戴夫的回答,我使用的解决方案是使用以下三行:

.antMatchers( "/admin**" ).hasAuthority("ADMIN" )
.antMatchers( "/admin/" ).hasAuthority( "ADMIN" )
.antMatchers( "/admin/**" ).hasAuthority( "ADMIN" )

这将在浏览器上呈现 403 错误。最终我会尝试让它重定向到错误页面或“/”。

【问题讨论】:

    标签: spring-security spring-boot


    【解决方案1】:

    您只显式保护了“/admin/”(带有尾部斜杠)。我想如果您正在访问“/admin”(没有尾部斜杠),您需要比这更精确。

    【讨论】:

    • 谢谢戴夫。实际上,我必须添加以下两行以不呈现页面: .antMatchers( "/admin**" ).hasRole( "ADMIN" ) && .antMatchers( "/admin/**" ).hasRole( "ADMIN" ) 否则, "/admin/ url 将呈现
    猜你喜欢
    • 2019-02-21
    • 2016-12-26
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2021-10-29
    • 2016-12-12
    相关资源
    最近更新 更多