spring boot +spring security 4 报type=Forbidden, status=403

橘子味阳光 2016-12-14 02:55:18
访问 resource/templates/login.html 报type=Forbidden, status=403

@SpringBootApplication
public class MainApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
//SpringApplication.run(MainApplication.class, args);
SpringApplication app=new SpringApplication(MainApplication.class);
Appctx.ctx=app.run(args);
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/").setViewName("login");
super.addViewControllers(registry);
}
}



public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customUserDetailsService;//code1

@Override
protected void configure(HttpSecurity http) throws Exception {
//允许所有用户访问”/”和”/home”
http
.csrf().disable()
.authorizeRequests().antMatchers("/", "/home").permitAll()
//其他地址的访问均需验证权限
.anyRequest().authenticated()
.and()
.formLogin()
//指定登录页是”/login”
.loginPage("/login")
//.usernameParameter("TEST").passwordParameter("123456")
.permitAll()
//登录成功后可使用loginSuccessHandler()存储用户信息,可选。
.successHandler(loginSuccessHandler())//code3
.and()
.logout()
//退出登录后的默认网址是”/home”
.logoutSuccessUrl("/home")
.permitAll()
.invalidateHttpSession(true)
.and()
//登录后记住用户,下次自动登录
//数据库中必须存在名为persistent_logins的表
//建表语句见code15
.rememberMe()
.tokenValiditySeconds(1209600);


}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//auth.inMemoryAuthentication().withUser("TEST").password("123456").roles("ADMIN");
auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());//code5
auth.eraseCredentials(false);
}
}
...全文
1092 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
三水写代码 2019-03-22
  • 打赏
  • 举报
回复
这是因为Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
现如今Spring Security中密码的存储格式是“{id}…………”。前面的id是加密方式,id可以是bcrypt、sha256等,后面跟着的是加密后的密码。也就是说,程序拿到传过来的密码的时候,会首先查找被“{”和“}”包括起来的id,来确定后面的密码是被怎么样加密的,如果找不到就认为id是null。程序后台会报错:There is no PasswordEncoder mapped for the id “null”。

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//inMemoryAuthentication 从内存中获取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("user1").password(new BCryptPasswordEncoder().encode("123456")).roles("USER");
}
痴人了个说梦 2017-08-07
  • 打赏
  • 举报
回复
楼主解决了吗?发一下解决方案吧

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧