我在使用方法级别注释设置我的应用程序时遇到了一些问题,@EnableGlobalMethodSecurity我正在使用Servlet 3.0样式初始化
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(MultiSecurityConfig.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了两种不同的方法来初始化AuthenticationManager两者都有自己的问题.请注意,在成功启动服务器时不使用@EnableGlobalMethodSecurity结果,并且所有表单安全性都按预期执行.当我在控制器上添加@EnableGlobalMethodSecurity和@PreAuthorize("hasRole('ROLE_USER')")注释时,我的问题出现了.
我试图独立设置基于表单和基于api的安全性.基于注释的方法仅需要用于api安全性.
一种配置如下.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class MultiSecurityConfig {
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/**").httpBasic();
}
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}
}
@Configuration
public static class FormWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
public void configure(WebSecurity web) throws …Run Code Online (Sandbox Code Playgroud) 我对Spring安全性的基本概念很少.我想为一个应用程序开发多个微服务,我想要在所有微服务中使用一点认证和授权.为了使其更具体,我想创建一个通用的Filter,它只能在每个微服务应用程序的web.xml中使用.我不想在所有微服务中使用以下内容.
<http>
<intercept-url pattern="/index*" access="ROLE_USER" />
<access-denied-handler error-page="/customaccessdenied.jsp" />
<intercept-url pattern="/manage*" access="ROLE_ADMIN" />
<form-login default-target-url="/index" always-use-default-target="true"/>
<logout/>
<!-- <remember-me/> -->
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="admin" authorities="ROLE_ADMIN,ROLE_USER" />
<user name="deb" password="deb" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
web.xml
=======
<!-- Spring Security -->
<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>
Run Code Online (Sandbox Code Playgroud)
我不想在所有微服务中使用上述内容.我的应用程序应该如下图所示.
请帮我如何实现它,如果您有任何样品申请,请提供链接.同样,重点是将所有与安全性相关的东西放在一个地方,并使用该库和自定义过滤器仅使用所有微服务的web.xml.
我想实现这样的目标
<filter>
<filter-name>mySecurityFilterChain</filter-name>
<filter-class>com.world.india.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>mySecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
在这方面我需要你的帮助.希望它对其他人有所帮助.