我使用 spring security 創(chuàng)建了一個(gè)項(xiàng)目。在configure(HttpSecurity http)我為用戶設(shè)置訪問"/home"權(quán)限時(shí),但在我登錄后,它顯示:403禁止訪問我創(chuàng)建了一個(gè)Entity class named User implementing UserDetails并且getAuthorities()我只是重新運(yùn)行Arrays.asList(new SimpleGrantedAuthority("USER"));對(duì)于 http 對(duì)象,我嘗試使用直接.hasRole('USER')方法而不是.access("hasRole('USER')"),問題是一樣的。@Overrideprotected void configure(HttpSecurity http) throws Exception{ http .authorizeRequests() .antMatchers("/home") .access("hasRole('USER')") .antMatchers("/","/**").access("permitAll") .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic();}
1 回答

繁花不似錦
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
您需要使用權(quán)限而不是角色。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").hasAuthority("USER")
.antMatchers("/","/**").access("permitAll")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
添加回答
舉報(bào)
0/150
提交
取消