第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何修復(fù)Spring Security中的角色?

如何修復(fù)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");}這是問題所在:假設(shè)我們的數(shù)據(jù)庫中有兩個用戶(一個是user角色,另一個是admin角色),一個是管理員,第二個是用戶,問題是當(dāng)我以用戶身份(只有user角色)連接時可以訪問管理資源(這不是預(yù)期的行為)。我認(rèn)為此查詢中的問題:"select username, password, 1 from users where username=?" 根據(jù)那username是主鍵?如果有人知道如何解決此問題?
查看完整描述

2 回答

?
ITMISS

TA貢獻(xiàn)1871條經(jīng)驗 獲得超8個贊

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


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


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");

}


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

TA貢獻(xiàn)2037條經(jīng)驗 獲得超6個贊

問題在于配置時的規(guī)則順序HttpSecurity。發(fā)生的情況是當(dāng)請求傳入并到達(dá)


authorizeRequests().anyRequest().authenticated() 

由于用戶已通過身份驗證,因此永遠(yuǎn)不會進(jìn)入


.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");

}

它使用責(zé)任鏈模式。它將遍歷規(guī)則鏈,直到找到匹配的規(guī)則。永遠(yuǎn)不會達(dá)到匹配規(guī)則之后的任何規(guī)則。通常,在編寫用于經(jīng)過身份驗證的請求的規(guī)則時,將優(yōu)先考慮更具體的規(guī)則。


查看完整回答
反對 回復(fù) 2019-11-04
  • 2 回答
  • 0 關(guān)注
  • 756 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號