首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaConfiguration for Spring4.0+Security3.2+ j_spring_security_check

JavaConfiguration for Spring4.0+Security3.2+ j_spring_security_check
EN

Stack Overflow用户
提问于 2013-12-20 13:25:08
回答 1查看 14.7K关注 0票数 11
  1. 创建登录页面 <%@ taglib prefix="c“uri="http://java.sun.com/jsp/jstl/core”%> Test Login ”method="POST"> Username Password uri=登录失败。原因:
  2. 声明一个WebSecurityConfigurer ,这里是我缺少j_username和j_password的地方。 @Configuration @EnableWebSecurity @ComponentScan(basePackages = {"com.sample.init.security"})公共类WebSecurityConfigurer扩展WebSecurityConfigurerAdapter {@;@Inject (AuthenticationManagerBuilder auth)抛出异常{com.sample.init.security}@重写受保护的空配置(HttpSecurity http)引发异常{ http .authorizeRequests() .antMatchers( "/resources/**“、"/static/**”、"/j_spring_security_check“),).permitAll() .anyRequest().authenticated() .and() .formLogin() .usernameParameter("j_username") /*默认为用户名!!*/ .passwordParameter("j_password") /* BY默认值是密码!!*/ .loginProcessingUrl("/j_spring_security_check") .loginPage("/") .defaultSuccessUrl("/page") .permitAll() .and() .logout() .permitAll();}@重写公共空配置(WebSecurity web)抛出异常{ web .ignoring() .antMatchers("/static/**");}
  3. 声明一个WebMvcConfigurer @EnableWebMvc @Configuration @ComponentScan(basePackages ={ "com.app.controller“、"com.app.service”、"com.app.dao“})公共类WebMvcConfigurer扩展WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver =新InternalResourceViewResolver();viewResolver.setPrefix(”/WEB/view/“);viewResolver.setSuffix(".jsp");返回viewResolver;}@覆盖公共无效registry.addViewController("/page").setViewName("page");addViewControllers(ViewControllerRegistry注册表){ViewControllerRegistry}@重写公共无效addResourceHandlers(ResourceHandlerRegistry注册表){ registry.addResourceHandler("static/**").addResourceLocations("static/");}}
  4. 声明安全初始化程序 公共类SecurityWebAppInitializer扩展AbstractSecurityWebApplicationInitializer {}
  5. 声明 公共类初始化器扩展{@重写受保护的Class[]{WebSecurityConfigurer.class};}@覆盖受保护的Class[]{WebMvcConfigurer.class,DataSourceConfigurer.class};}@覆盖受保护的String[] getServletMappings() {返回新的String[]{"/"};}}
  6. 实现自定义身份验证提供程序 @ComponentScan(basePackages = {"com.app.service"})公共类CustomAuthenticationProvider实现AuthenticationProvider {私有静态最终日志=AuthenticationProvider @Inject私有AppService服务;@重写公共身份验证(身份验证)抛出AuthenticationException { //Thread.dumpStack();字符串用户名= authentication.getName();字符串密码= authentication.getCredentials().toString();String message =String.format(用户名:'%s‘密码:’‘%s’,用户名,密码);userBean UserBean =service.validate(用户名、密码);LOG.debug(消息);if (userBean != null) { List grantedAuths =新ArrayList<>();grantedAuths.add(新SimpleGrantedAuthority(“用户”));返回新UsernamePasswordAuthenticationToken(userBean,身份验证,grantedAuths);{ String =String.format(“无效凭据%s",消息);抛出新的BadCredentialsException(错误);}@覆盖公共布尔支持(Class<?>身份验证){返回Class<?>}}

我跳过了EchoController,AppService,AppDao和UserBean。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-25 13:31:18

在3.2个版本中,post参数已从j_username更改为用户名,j_password更改为密码。登录url也从/j_spring_security_check更改为/login。

有关为什么实现此更改的说明,请参阅此链接:http://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/reference/htmlsingle/#jc-httpsecurity。这些变化如下:

  • GET /login呈现登录页而不是/spring_security_login
  • POST /login认证用户而不是/j_spring_security_check
  • 用户名参数默认为用户名,而不是j_username。
  • 密码参数默认为密码,而不是j_password。

下面是一个登录表单的示例:http://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/reference/htmlsingle/#jc-form

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20704656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档