基于Java注释的Spring安全配置
我尝试执行基于基于Java注释的Spring安全配置,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我尝试执行基于Java注释的Springsecurity配置。我在遵循教程后执行此操作,并按照提供的代码执行此操作 @Configuration @EnableWebSecurity // need to change this to the security directory @ComponentScan("") public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired pri
Java
注释的Springsecurity
配置。我在遵循教程后执行此操作,并按照提供的代码执行此操作
@Configuration
@EnableWebSecurity
// need to change this to the security directory
@ComponentScan("")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
@Autowired
private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication()
.withUser("temporary").password("temporary").roles("ADMIN")
.and()
.withUser("user").password("userPass").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint)
.and()
.authorizeRequests()
.antMatchers("/api/foos").authenticated()
.and()
.formLogin()
.successHandler(authenticationSuccessHandler)
.failureHandler(new SimpleUrlAuthenticationFailureHandler())
.and()
.logout();
}
@Bean
public MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler() {
return new MySavedRequestAwareAuthenticationSuccessHandler();
}
@Bean
public SimpleUrlAuthenticationFailureHandler myFailureHandler() {
return new SimpleUrlAuthenticationFailureHandler();
}
}
我工作的项目的API
基础
public static final String API_BASE = "/*";
例如,我执行cURL
请求,如下所示:
curl -X GET http://localhost:8080/rest/wallet/wallets | json
我不确定代码中的
.antMatchers(“/api/foos”).authenticated()
行。例如,来自foos
的地方,我是否需要将其更改为类似.antMatchers(“/foos”).authenticated()
?如果您是编程新手,这是一个有效的问题。但是要习惯它。所有示例通常都将“foo”和“bar”作为示例变量、方法名称等
无论如何,.antMatchers(“/api/foos”).authenticated()
指定需要对匹配/api/foo的模式URL进行身份验证,然后应使用以下处理程序
将模式更改为您的匹配模式-.antMatchers(“/rest/wallet/**”)
并测试您的代码
如需更多参考资料,请阅读本文:如果您是编程新手,这是一个有效的问题。但是要习惯它。所有示例通常都将“foo”和“bar”作为示例变量、方法名称等 无论如何,
.antMatchers(“/api/foos”).authenticated()
指定需要对匹配/api/foo的模式URL进行身份验证,然后应使用以下处理程序
将模式更改为您的匹配模式-.antMatchers(“/rest/wallet/**”)
并测试您的代码
更多参考-请阅读此帖子:因此我有类似于
@path(“rest/wallet”)
、@path(“rest/service”)
和@path(“rest/user”)
的路径,我使用了类似于.antMatchers(“rest/wallet/**”).authenticated().antMatchers(“rest/user/**”)的匹配。authenticated().antMatchers(“rest/service/**”).authenticated()
。现在正确吗?让我知道,我会接受你的答案。我觉得很好。只需使用/like/rest/wallet启动URL上下文路径(考虑将根路径@path(“/rest”)放在类的顶部,这样你就不必一直重复根上下文)。我只是给了你一个观察的环境。探索更多,继续前进。你被卡住的次数越多,学到的东西就越多。:)这确实适用于编程,有时你只是感觉不到推动更多的能量:)所以我有类似@path(“rest/wallet”)
,@path(“rest/service”)
和@path(“rest/user”)的路径
我使用了类似于.antMatchers(“rest/wallet/**”).authenticated().antMatchers(“rest/user/**”).authenticated().antMatchers(“rest/service/**”).authenticated()
。现在正确吗?让我知道,我会接受你的答案。我觉得很好。只需使用/like/rest/wallet启动URL上下文路径(考虑将根路径@path(“/rest”)放在类的顶部,这样你就不必一直重复根上下文).我只是给了你一个要注意的环境。多探索,继续前进。你越陷越深,学到的东西就越多。:)编程确实如此,有时你只是感觉不到推动更多的能量:)