我正在从参考资料中学习弹簧安全性.发布3.1.2.RELEASE.如上所述,我已经配置了这样的security:http标签
安全的context.xml
<security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>
Run Code Online (Sandbox Code Playgroud)
web.xml中
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*-context.xml</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>security</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>security</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
安全servlet.xml中
<context:component-scan base-package="com.pokuri.security.mvc.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
但是当我启动应用程序时,我得到了这个异常.如果我删除安全配置我的春季Web应用程序工作正常.我在stackoverflow中遇到了同样的问题.但没有运气.
JavaDoc文档:
将此批注添加到
@Configuration类中,以通过扩展基类和覆盖单个方法来Spring Security定义任何WebSecurityConfigurer或更多可能的配置WebSecurityConfigurerAdapter.
JavaDoc文档:
将此批注添加到
@Configuration类中以使Spring Security配置与其集成Spring MVC.
到目前为止我所研究的大多数人都说Apache Shiro易于使用且易于与CAS集成(用于SSO等).只是问一下是否有人有使用它们和使用哪一个的经验以及为什么一个比其他更好?
Spring Boot 2.6.x 似乎引入了一些更改,导致之前与 Keycloak 的集成出现循环引用,从而阻止应用程序启动;它可以在当前的 2.5.x 版本中正常工作并启动。
\n明确地说,通过在spring-boot-starter-parent中将<version> 标记值从 2.5.7 更改为 2.6.1 之外的任何内容,都会出现下面详细说明的错误/消息。
\n当然,预期的行为是应用程序正常启动,并且像以前一样使用 Keycloak 进行保护。
\n实际的消息是:
\n***************************\nAPPLICATION FAILED TO START\n***************************\n\nDescription:\n\nThe dependencies of some of the beans in the application context form a cycle:\n\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80->\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n| keycloakSecurityConfig (field private org.keycloak.adapters.KeycloakConfigResolver org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter.keycloakConfigResolver)\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80<-\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n完整的堆栈跟踪:
\n2021-11-30 12:49:07.308 DEBUG 7 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : Application failed to start due to an exception\n\norg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name \'keycloakSecurityConfig\': Requested bean is currently in creation: Is …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个主要使用Spring提供REST API的webapp,并且我正在尝试配置安全端.
我正在尝试实现这种模式:https://developers.google.com/accounts/docs/MobileApps(Google已完全更改了该页面,因此不再有意义 - 请参阅我在此处引用的页面:http: //web.archive.org/web/20130822184827/https://developers.google.com/accounts/docs/MobileApps)
这是我需要做的事情:
(例如,用户使用普通表单登录/注册,webapp提供带有令牌的安全cookie,然后可以在以下API请求中使用)
我有一个正常的身份验证设置如下:
@Override protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/mobile/app/sign-up").permitAll()
.antMatchers("/v1/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/loginprocess")
.failureUrl("/?loginFailure=true")
.permitAll();
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑添加一个pre-auth过滤器,它检查请求中的令牌然后设置安全上下文(这是否意味着将跳过正常的后续身份验证?),但是,超出了我的正常用户/密码基于令牌的安全性没有做太多,但基于其他一些例子,我想出了以下内容:
安全配置:
@Override protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.addFilter(restAuthenticationFilter())
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint()).and()
.antMatcher("/v1/**")
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/mobile/app/sign-up").permitAll()
.antMatchers("/v1/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/loginprocess")
.failureUrl("/?loginFailure=true")
.permitAll();
}
Run Code Online (Sandbox Code Playgroud)
我的定制休息过滤器:
public class RestAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
public RestAuthenticationFilter(String …Run Code Online (Sandbox Code Playgroud) 我正在使用包含STOMP/SockJS WebSocket的Spring Boot(1.3.0.BUILD-SNAPSHOT)设置RESTful Web应用程序,我打算从iOS应用程序和Web浏览器中使用它.我想使用JSON Web令牌(JWT)来保护REST请求和WebSocket接口,但我对后者有困难.
该应用程序使用Spring Security进行保护: -
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
public WebSecurityConfiguration() {
super(true);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("steve").password("steve").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling().and()
.anonymous().and()
.servletApi().and()
.headers().cacheControl().and().and()
// Relax CSRF on the WebSocket due to needing direct access from apps
.csrf().ignoringAntMatchers("/ws/**").and()
.authorizeRequests()
//allow anonymous resource requests
.antMatchers("/", "/index.html").permitAll()
.antMatchers("/resources/**").permitAll()
//allow anonymous POSTs to JWT
.antMatchers(HttpMethod.POST, "/rest/jwt/token").permitAll()
// Allow anonymous …Run Code Online (Sandbox Code Playgroud) 我想向任何登录的用户显示内容,如果他们没有登录则要隐藏.我正在使用jsp和spring安全性.
显然,一个成熟的解决方案很容易完成.但实现这一目标的最简洁的标准方法是什么?
Spring安全标签似乎没有很好的方法可以在将来添加新角色.
我正在使用Spring MVC和Spring Security 3.0.6.RELEASE.在JSP中获取用户名的最简单方法是什么?或者甚至只是用户是否登录?我可以想到几个方面:
使用这样的scriptlet来确定用户是否已登录:
<%=org.springframework.security.core.context.SecurityContextHolder.getContext()
.getAuthentication().getPrincipal().equals("anonymousUser")
? "false":"true"%>
Run Code Online (Sandbox Code Playgroud)
我不喜欢使用scriptlet,我想在一些<c:if>标签中使用它,这需要将其作为页面属性放回去.
我可以再次使用我的SecurityContextHolder @Controller并将其放在模型上.不过,我在每个页面都需要这个,所以我宁愿不必在每个控制器中添加这个逻辑.
我怀疑有一个更清洁的方法来做到这一点......
我想基于使用Spring Security JSP标记库的角色有条件地显示一些内容.但在Spring Security 3.1.x中只检查一个角色.
我可以使用,但ifAllGranted已被弃用.
有帮助吗?
我有一个Spring Boot web应用程序,配置了spring security.我想暂时禁用身份验证(直到需要).
我把它添加到application.properties:
security.basic.enable: false
management.security.enabled: false
Run Code Online (Sandbox Code Playgroud)
这是我的一部分
但我仍然有一个基本的安全性:启动时生成一个默认的安全密码,我仍然收到HTTP身份验证提示框.
我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.test.sample</groupId>
<artifactId>navigo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<properties>
<java.version>1.7</java.version>
<jsoup.version>1.8.3</jsoup.version>
<guava.version>18.0</guava.version>
<postgresql.version>9.3-1103-jdbc41</postgresql.version>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> …Run Code Online (Sandbox Code Playgroud) spring ×10
spring-security ×10
java ×6
spring-mvc ×5
jsp ×2
spring-boot ×2
apache ×1
jwt ×1
keycloak ×1
rest ×1
security ×1
shiro ×1
sockjs ×1
websocket ×1