为什么permitAll()不起作用,并要求在请求中提供认证对象?

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired 
private CustomUserDetailService userDetailsService; 

@Bean 
public PasswordEncoder passwordEncoder() { 
 return new StandardPasswordEncoder(); 
} 

@Override 
protected void configure(AuthenticationManagerBuilder auth) 
 throws Exception { 
 auth.authenticationProvider(authenticationProvider()); 
} 

@Bean 
public DaoAuthenticationProvider authenticationProvider() { 
 DaoAuthenticationProvider authProvider 
  = new DaoAuthenticationProvider(); 
 authProvider.setUserDetailsService(userDetailsService); 
 authProvider.setPasswordEncoder(encoder()); 
 return authProvider; 
} 

@Bean 
public ShaPasswordEncoder encoder() { 
 return new ShaPasswordEncoder(256); 
} 

@Override 
public void configure(WebSecurity web) throws Exception {} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
 http.csrf().disable(); 

} 

@Override 
@Bean 
public AuthenticationManager authenticationManagerBean() throws Exception { 
 return super.authenticationManagerBean(); 
} 

@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true) 
private static class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration { 

 public GlobalSecurityConfiguration() { 
 } 
 @Override 
 protected MethodSecurityExpressionHandler createExpressionHandler() { 
  return new OAuth2MethodSecurityExpressionHandler(); 
 } 

} 

}
当我在做邮递员http://localhost:8889/secure?lang=en电话,我得到错误:

{
“error”: “unauthorized”,
“error_description”: “An Authentication object was not found in the SecurityContext”
}
我已经配置/secure端点permitAll(),所以它不应该要求通过访问令牌。为什么permitAll()在这种情况下不起作用?

我也尝试通过允许所有请求,但它也没有工作,并面对同样的错误作出回应。

@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll();

认证和授权是两回事。 'permitAll’适用于授权。您仍然需要进行身份验证。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的 Spring Boot 权限认证示例代码: 首先,我们需要在 pom.xml 文件添加 Spring Security 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 然后,在 Spring Boot 应用程序的主类上添加 @EnableWebSecurity 注解,以启用 Spring Security: ```java @SpringBootApplication @EnableWebSecurity public class MyApp { // ... } ``` 接下来,我们需要创建一个继承自 WebSecurityConfigurerAdapter 的配置类,用于配置 Spring Security: ```java @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/") .permitAll() .and() .logout() .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 在上面的代码,我们配置了一个基本的权限认证功能,包括登录、校验、校验不通过跳转登录页面等功能。具体来说: - antMatchers("/login").permitAll() 表示允许所有用户访问 /login 页面。 - anyRequest().authenticated() 表示除了 /login 页面之外的所有请求都需要进行身份验证。 - formLogin() 表示使用表单登录进行身份验证。 - loginPage("/login") 表示登录页面的 URL。 - defaultSuccessUrl("/") 表示登录成功后跳转的 URL。 - logout() 表示退出登录功能。 最后,我们需要创建一个实现了 UserDetailsService 接口的类,用于从数据库或其他数据源获取用户信息: ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found"); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), new ArrayList<>()); } } ``` 在上面的代码,我们从 UserRepository 获取用户信息,并将其转换为 Spring Security 的 User 对象。 这就是一个简单的 Spring Boot 权限认证示例代码。当用户访问需要身份验证的页面时,系统会自动跳转到登录页面,用户输入正确的用户名和密码后,系统会将用户信息存储在 Session ,并跳转到指定的页面。如果用户退出登录,系统会清除 Session 的用户信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值