我使用 spring security 創建了一個項目。在configure(HttpSecurity http)我為用戶設置訪問"/home"權限時,但在我登錄后,它顯示:403禁止訪問我創建了一個Entity class named User implementing UserDetails并且getAuthorities()我只是重新運行Arrays.asList(new SimpleGrantedAuthority("USER"));對于 http 對象,我嘗試使用直接.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貢獻1851條經驗 獲得超4個贊
您需要使用權限而不是角色。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").hasAuthority("USER")
.antMatchers("/","/**").access("permitAll")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
添加回答
舉報
0/150
提交
取消