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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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

2 回答

?
ITMISS

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

anyRequest()始終應(yīng)用第一個(gè)匹配器,因?yàn)槠ヅ淦鞯捻樞蚝苤匾?qǐng)參見HttpSecurity#authorizeRequests:


注意匹配器是按順序考慮的。因此,以下內(nèi)容無(wú)效,因?yàn)榈谝粋€(gè)匹配器匹配每個(gè)請(qǐng)求,并且永遠(yuǎn)不會(huì)到達(dá)第二個(gè)映射:


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

            .hasRole("ADMIN")

您修改和簡(jiǎn)化的配置:


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

}


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

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

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


authorizeRequests().anyRequest().authenticated() 

由于用戶已通過(guò)身份驗(yàn)證,因此永遠(yuǎn)不會(huì)進(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)不會(huì)達(dá)到匹配規(guī)則之后的任何規(guī)則。通常,在編寫用于經(jīng)過(guò)身份驗(yàn)證的請(qǐng)求的規(guī)則時(shí),將優(yōu)先考慮更具體的規(guī)則。


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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