我想使用通用方法来管理5xx错误代码,让我们具体说明db在整个spring应用程序中出现故障的情况.我想要一个漂亮的错误json而不是堆栈跟踪.
对于控制器,我有一个@ControllerAdvice用于不同异常的类,这也是捕获db在请求中间停止的情况.但这并不是全部.我也碰巧有一个自定义CorsFilter扩展OncePerRequestFilter,当我打电话给doFilter我得到CannotGetJdbcConnectionException它,它将不会被管理@ControllerAdvice.我在线阅读了几件让我更加困惑的事情.
所以我有很多问题:
ExceptionTranslationFilter但这只是处理AuthenticationException或AccessDeniedException.HandlerExceptionResolver,但这让我怀疑,我没有任何自定义异常来管理,必须有一个比这更明显的方法.我还尝试添加一个try/catch并调用一个实现HandlerExceptionResolver(应该足够好,我的异常没什么特别的)但是这不会在响应中返回任何内容,我得到一个状态200和一个空体.有什么好方法可以解决这个问题吗?谢谢
我很难理解JAAS.这一切似乎都应该比它应该更复杂(尤其是Sun教程).我需要一个简单的教程或示例,介绍如何在基于Struts + Spring + Hibernate和自定义用户存储库的Java应用程序中实现安全性(身份验证+授权).可以使用ACEGI实现.
我正在使用Spring MVC @ControllerAdvice并@ExceptionHandler处理REST Api的所有异常.它适用于Web mvc控制器抛出的异常,但它不适用于spring安全自定义过滤器抛出的异常,因为它们在调用控制器方法之前运行.
我有一个自定义弹簧安全过滤器,它执行基于令牌的身份验证:
public class AegisAuthenticationFilter extends GenericFilterBean {
...
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
try {
...
} catch(AuthenticationException authenticationException) {
SecurityContextHolder.clearContext();
authenticationEntryPoint.commence(request, response, authenticationException);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用此自定义入口点:
@Component("restAuthenticationEntryPoint")
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint{
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
并使用此类来处理全局异常:
@ControllerAdvice
public class RestEntityResponseExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({ InvalidTokenException.class, AuthenticationException.class })
@ResponseStatus(value = HttpStatus.UNAUTHORIZED) …Run Code Online (Sandbox Code Playgroud) 我在我的jsp上有CKeditor,每当我上传一些东西时,会弹出以下错误:
Refused to display 'http://localhost:8080/xxx/xxx/upload-image?CKEditor=text&CKEditorFuncNum=1&langCode=ru' in a frame because it set 'X-Frame-Options' to 'DENY'.
Run Code Online (Sandbox Code Playgroud)
我试过删除Spring Security,一切都像魅力一样.如何在spring security xml文件中禁用它?我应该在<http>标签之间写什么
我正在学习通过做一个小项目来使用Spring Boot.目前我得到了主要课程:
package com.recweb.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
/*@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})*/
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
成员类(id,firstname ..),MemberController类:
package com.recweb.springboot;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberController {
@GetMapping("/members")
public List<Member> getAllUsers() {
return Arrays.asList(new Member(1, "amit"));
}
}
Run Code Online (Sandbox Code Playgroud)
和WebSecurityConfig类:
package com.recweb.springboot;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用权限设置基于角色的安全性.我正在尝试与Spring-Security一起完成这项工作.
我不想设置ACL,因为它似乎对我的要求有点过分.
我只想拥有本文所述的简单权限和角色.遗憾的是,该文章没有描述如何实现给定的解决方案.
有人已经尝试过这个并且可以指出我正确的方向吗?也许还有另一篇描述实施的博客文章?
非常感谢你.
我正在使用Spring Security 3.2和Spring 4.0.1
我正在努力将xml配置转换为Java配置.当我在我的过滤器中注释AuthenticationManager时@Autowired,我得到一个例外
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过注射AuthenticationManagerFactoryBean但是也失败了类似的例外.
这是我正在使用的XML配置
<?xml version="1.0" encoding="UTF-8"?> <beans ...>
<security:authentication-manager id="authenticationManager">
<security:authentication-provider user-service-ref="userDao">
<security:password-encoder ref="passwordEncoder"/>
</security:authentication-provider>
</security:authentication-manager>
<security:http
realm="Protected API"
use-expressions="true"
auto-config="false"
create-session="stateless"
entry-point-ref="unauthorizedEntryPoint"
authentication-manager-ref="authenticationManager">
<security:access-denied-handler ref="accessDeniedHandler"/>
<security:custom-filter ref="tokenAuthenticationProcessingFilter" position="FORM_LOGIN_FILTER"/>
<security:custom-filter ref="tokenFilter" position="REMEMBER_ME_FILTER"/>
<security:intercept-url method="GET" pattern="/rest/news/**" access="hasRole('user')"/>
<security:intercept-url method="PUT" pattern="/rest/news/**" access="hasRole('admin')"/>
<security:intercept-url method="POST" …Run Code Online (Sandbox Code Playgroud) 我知道保护REST API是一个广泛评论的主题,但我无法创建符合我的标准的小型原型(我需要确认这些标准是现实的).如何保护资源以及如何使用Spring安全性有很多选择,我需要澄清我的需求是否切合实际.
我的要求
当前状态
我的REST API运行良好,但现在我需要保护它.当我在寻找解决方案时,我创建了一个javax.servlet.Filter过滤器:
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String accessToken = request.getHeader(AUTHORIZATION_TOKEN);
Account account = accountDao.find(accessToken);
if (account == null) {
throw new UnauthorizedException();
}
chain.doFilter(req, res);
}
Run Code Online (Sandbox Code Playgroud)
但是这个解决方案javax.servlet.filters不能正常工作,因为@ControllerAdviceSpring 存在异常处理问题servlet dispatcher.
我需要的
我想知道这些标准是否切合实际并获得任何帮助,如何开始使用Spring Security保护REST API.我阅读了许多教程(例如Spring Data REST + Spring Security),但都在非常基本的配置中工作 - 具有凭据的用户在配置 …
不确定这是否是Spring 5.0.3的一个错误或一个新功能来解决我的问题.
升级后,我收到此错误.有趣的是,此错误仅在我的本地计算机上.使用HTTPS协议的测试环境上的相同代码工作正常.
继续...
我收到此错误的原因是因为我加载生成的JSP页面的URL是/location/thisPage.jsp.评估代码request.getRequestURI()给了我结果/WEB-INF/somelocation//location/thisPage.jsp.如果我将JSP页面的URL修复为此location/thisPage.jsp,则工作正常.
所以我的问题是,我应该/从JSP代码中的路径中删除,因为这是未来需要的.或者Spring引入了一个错误,因为我的机器和测试环境之间的唯一区别是协议HTTP与HTTPS.
org.springframework.security.web.firewall.RequestRejectedException:请求被拒绝,因为URL未规范化.org.springframework.security.web.firewall.StrictHttpFirewall.getFirewalledRequest(StrictHttpFirewall.java:123)org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:194)org.springframework.security.web.FilterChainProxy .doFilter(FilterChainProxy.java:186)org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
Spring Security可以使用@PreAuthorizeSpring控制器方法吗?
spring-security ×10
java ×7
spring ×7
spring-mvc ×3
spring-boot ×2
controller ×1
jaas ×1
maven ×1
rest ×1
security ×1