Con*_*asy 5 java spring-security static-resource
通过此设置,我可以完美地提供静态资源,但是我必须逐个文件地定义允许提供服务的文件。
我当前的用例是目录中的任何内容都/resources/public/应该允许客户端访问。
我已经尝试过 one liner /resources/public/**,但/public/**仍然不允许访问我收到 403 的所有公共资源。因此,在我的 http 配置中,我已经开始定义允许的文件扩展名,但我不喜欢这种方法,因为我的 web 应用程序中有很多不同的扩展名。
我的问题是,如何允许访问所有文件,而/resources/public/不必为每个文件扩展名定义 ant 匹配器,或者我只是小气?
Spring WebSecurityConfigurerAdapter- 根据 jmw5598 的答案进行编辑。
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
@Override
protected void configure(HttpSecurity http) {
http
.authorizeRequests()
.authorizeRequests()
.antMatchers(
"/public/**",
"/.svg", "/.ico", "/.eot", "/.woff2",
"/.ttf", "/.woff", "/.html", "/.js",
"/.map", "/*.bundle.*",
"/index.html", "/", "/home", "/dashboard")
.permitAll()
.anyRequest().authenticated();
}
Run Code Online (Sandbox Code Playgroud)
服务网络应用程序的控制器:
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@Controller
public class AngularWebAppController {
@GetMapping(value = "/{path:[^\\.]*}")
public String redirect() {
return "forward:/";
}
}
Run Code Online (Sandbox Code Playgroud)
我的目录结构在/resources
您想要请求分隔符资源或 URL 处理程序映射。这在春天很容易。
\n\nServlet上下文
\n\n<!-- Handles HTTP GET requests for /resources/** by efficiently serving \n up static resources in the ${webappRoot}/resources directory -->\n\n<resources mapping="/resources/**" location="/resources/" />\nRun Code Online (Sandbox Code Playgroud)\n\n和
\n\n<default-servlet-handler />\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n\n\n此标记允许将 DispatcherServlet 映射到“/”(从而覆盖容器默认 Servlet 的映射),同时仍允许容器默认 Servlet 处理静态资源请求 [ ...]
\n
也许您对这个春季安全内容有用。
\n\n自定义WebSecurityConfigurerAdapter
\n\n我们的 HelloWebSecurityConfiguration 示例演示了 Spring Security Java 配置可以为我们提供一些非常好的默认值。让\xe2\x80\x99s 看一下一些基本的定制。
\n\n@EnableWebSecurity\n@Configuration\npublic class CustomWebSecurityConfigurerAdapter extends\n WebSecurityConfigurerAdapter {\n @Autowired\n public void configureGlobal(AuthenticationManagerBuilder auth) {\n auth\n .inMemoryAuthentication()\n .withUser("user") // #1\n .password("password")\n .roles("USER")\n .and()\n .withUser("admin") // #2\n .password("password")\n .roles("ADMIN","USER");\n }\n\n @Override\n public void configure(WebSecurity web) throws Exception {\n web\n .ignoring()\n .antMatchers("/resources/**"); // #3\n }\n\n @Override\n protected void configure(HttpSecurity http) throws Exception {\n http\n .authorizeUrls()\n .antMatchers("/signup","/about").permitAll() // #4\n .antMatchers("/admin/**").hasRole("ADMIN") // #6\n .anyRequest().authenticated() // 7\n .and()\n .formLogin() // #8\n .loginUrl("/login") // #9\n .permitAll(); // #5\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n假设我们调整 AbstractAnnotationConfigDispatcherServletInitializer 来加载新配置,我们的 CustomWebSecurityConfigurerAdapter 将执行以下操作:
\n\n对于熟悉基于 XML 的配置的人来说,上面的配置与以下 XML 配置非常相似:
\n\n<http security="none" pattern="/resources/**"/>\n<http use-expressions="true">\n <intercept-url pattern="/logout" access="permitAll"/>\n <intercept-url pattern="/login" access="permitAll"/>\n <intercept-url pattern="/signup" access="permitAll"/>\n <intercept-url pattern="/about" access="permitAll"/>\n <intercept-url pattern="/**" access="hasRole(\'ROLE_USER\')"/>\n <logout\n logout-success-url="/login?logout"\n logout-url="/logout"\n />\n <form-login\n authentication-failure-url="/login?error"\n login-page="/login"\n login-processing-url="/login"\n password-parameter="password"\n username-parameter="username"\n />\n</http>\n<authentication-manager>\n <authentication-provider>\n <user-service>\n <user name="user" \n password="password" \n authorities="ROLE_USER"/>\n <user name="admin" \n password="password" \n authorities="ROLE_USER,ROLE_ADMIN"/>\n </user-service>\n </authentication-provider>\n</authentication-manager>\nRun Code Online (Sandbox Code Playgroud)\n\n与 XML 命名空间的相似之处
\n\n在查看我们稍微复杂的示例之后,您可能会发现 XML 命名空间和 Java 配置之间的一些相似之处。以下是一些比较有用的要点:
\n\n