亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復Spring Security中的角色?

如何修復Spring Security中的角色?

慕的地6264312 2019-11-04 13:05:42
我試圖在我的項目中使用Spring Security,這是代碼:@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {    // TODO Auto-generated method stub    //super.configure(auth);    //auth.inMemoryAuthentication().withUser("admin").password("1111").roles("USER");    auth        .jdbcAuthentication()            .dataSource(dataSource)            .usersByUsernameQuery("select username, password, 1 from users where username=?")            .authoritiesByUsernameQuery("select users_username, roles_id  from roles_users where users_username=?")            .rolePrefix("ROLE_");}   @Overrideprotected void configure(HttpSecurity http) throws Exception {    http        .csrf().disable();          http        .httpBasic();    http        .authorizeRequests()            .anyRequest().authenticated();    http        .authorizeRequests()            .antMatchers("/users/all").hasRole("admin")            .and()        .formLogin();    http        .exceptionHandling().accessDeniedPage("/403");}這是問題所在:假設我們的數據庫中有兩個用戶(一個是user角色,另一個是admin角色),一個是管理員,第二個是用戶,問題是當我以用戶身份(只有user角色)連接時可以訪問管理資源(這不是預期的行為)。我認為此查詢中的問題:"select username, password, 1 from users where username=?" 根據那username是主鍵?如果有人知道如何解決此問題?
查看完整描述

2 回答

?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

anyRequest()始終應用第一個匹配器,因為匹配器的順序很重要,請參見HttpSecurity#authorizeRequests:


注意匹配器是按順序考慮的。因此,以下內容無效,因為第一個匹配器匹配每個請求,并且永遠不會到達第二個映射:


http.authorizeRequests().antMatchers("/**").hasRole("USER").antMatchers("/admin/**")

            .hasRole("ADMIN")

您修改和簡化的配置:


@Override

protected void configure(HttpSecurity http) throws Exception {

    http

        .csrf().disable()      

        .httpBasic()

            .and()

        .authorizeRequests()

            .antMatchers("/users/all").hasRole("admin")

            .anyRequest().authenticated()

            .and()

        .formLogin()

            .and()

        .exceptionHandling().accessDeniedPage("/403");

}


查看完整回答
反對 回復 2019-11-04
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

問題在于配置時的規則順序HttpSecurity。發生的情況是當請求傳入并到達


authorizeRequests().anyRequest().authenticated() 

由于用戶已通過身份驗證,因此永遠不會進入


.antMatchers("/users/all").hasRole("admin")

這是如何配置它的示例:


@Override

protected void configure(HttpSecurity http) throws Exception {

    http

        .csrf().disable()      

        .httpBasic()

        .and()

    .authorizeRequests()

        .antMatchers("/public").permitAll()

        .antMatchers("/user").hasRole("USER")

        .antMatchers("/admin").hasRole("ADMIN")

        .anyRequest().authenticated()

        .and()

    .formLogin()

        .and()

    .exceptionHandling().accessDeniedPage("/403");

}

它使用責任鏈模式。它將遍歷規則鏈,直到找到匹配的規則。永遠不會達到匹配規則之后的任何規則。通常,在編寫用于經過身份驗證的請求的規則時,將優先考慮更具體的規則。


查看完整回答
反對 回復 2019-11-04
  • 2 回答
  • 0 關注
  • 761 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號