gpt4 book ai didi

java - Spring Security for Rest @PreAuthorize 500错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:17 27 4
gpt4 key购买 nike

我想要休息的基本安全性,这是我的配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private PacijentUserDetailsService pacijent;
@Autowired
private FizioterapeutUserDetailsService fizioterapeut;
@Autowired
private FizijatarUserDetailsService fizijatar;

@Override
protected void configure(AuthenticationManagerBuilder
auth) throws Exception {
auth.userDetailsService(pacijent)
.passwordEncoder(new
BCryptPasswordEncoder());
auth.userDetailsService(fizioterapeut).passwordEncoder(new
BCryptPasswordEncoder());
auth.userDetailsService(fizijatar).passwordEncoder(new
BCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/pacijent/", "/fizijatar/","/fizioterapeut/").permitAll()
.antMatchers("/pacijent/**","/fizijatar/**","/fizioterapeut/**").authenticated()
.and()
.httpBasic()
.realmName("Ordinacija")
.and()
.csrf()
.disable();

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

}

我有 3 个 userdetailservice 的实现,这是一个例子:

  @Component
public class PacijentUserDetailsService implements UserDetailsService {

@Autowired
private PacijentService pacijentService;

@Override
public UserDetails loadUserByUsername(String jmbg) throws UsernameNotFoundException {
Pacijent pacijent = pacijentService.vratiPacijenta(jmbg);
if (pacijent == null) {
throw new UsernameNotFoundException(String.format("Pacijent nije pronadjen", jmbg));

}

List<GrantedAuthority> authorities = new ArrayList<>();
if (pacijentService.postojiPacijentPoJmbgu(jmbg)) {
authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
}

UserDetails userDetails = new org.springframework.security.core.userdetails.User(pacijent.getJmbg(),
pacijent.getSifra(), authorities);
return userDetails;
}
}

还有我的 web xml 文件:

           <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

org.springframework.web.context.ContextLoaderListener

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>

当我启动应用程序并转到具有 @PreAuthorize 方法的休息方法时,出现错误 500:请求处理失败;嵌套异常是 org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在 SecurityContex 中找不到 Authentication 对象。

最佳答案

我写答案是为了帮助别人。如果您有一个像 OncePerRequestFilter 这样的过滤器来检查身份验证,那么当您检查身份验证并且失败时,您可以这样设置:



 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "未经授权");

否则,如果你让 spring 管理身份验证,你可以使用异常处理程序:



<前>@ControllerAdvice
@RestController
公共(public)类 CustomExceptionHandler 扩展 ResponseEntityExceptionHandler {
@ExceptionHandler({AccessDeniedException.class})
公开 final
ResponseEntity handleUserNotFoundException(EntityNotFoundException ex, WebRequest 请求){
return new ResponseEntity<>("未经授权", HttpStatus.UNAUTHORIZED);
}
}

关于java - Spring Security for Rest @PreAuthorize 500错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37393074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com