Return to site

Request Method Post Not Supported Spring Security

broken image


2020腾讯云限时秒杀,爆款1核2G云服务器99元/年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062

2020阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods

推荐:HTTP Status 405 - Request method 'POST' not supported (Spring MVC)

HTTP Status 405 - Request method 'POST' not supported (Spring MVC) java native method

Spring Security Request method 'POST' not supported. Ask Question Asked 6 years ago. Active 3 years, 3 months ago. Request method 'POST' not supported. Spring Info: - Using Spring Security 3.2.5 - Using Spring Boot App to start up server. Spring-Java-Config. Codes: SecurityConfig.java. HTTP Status 405 - Request method 'POST' not supported - Spring Security. WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supported 00:02:34.523 http.

一. 什么是Native Method 简单地讲,一个Native Method就是一个java调用非java代码的接口。一个Native Method是这样一个java的方法:该方法的实现由非java语

answer 1 >>解决方法

I found the answer. Just add one more parameter in data when sending AJAX request is '_csrf' token key.

推荐:jsp spring mvc 上传时HTTP Status 405 - Request method 'GET' not supported是什么原因

jsp spring mvc 上传时HTTP Status 405 - Request method 'GET' not supported是什么原因 405:用来访问本页面的 HTTP 谓词不被允许(方法不被允许)请求方法(G

  • 1使用Spring Security实现权限管理
  • 2Spring Security3源码分析-UsernamePasswordAuthenticationFilter分析
  • 3Spring Security3源码分析-authentication-manager标签解析
  • 4Spring Security3源码分析-FilterChainProxy初始化
  • 5传智播客JAVA培训20100524SPRING SECURITY
  • 1微信公众号文章采集,并发布到WordPress
  • 2HTTP Status 405 - Request method 'POST' not supported (Spring MVC)
  • 3jsp spring mvc 上传时HTTP Status 405 - Request method 'GET' not supported是什么原因
  • 4java native method

最新文章

  • 1在阿里架构师眼中构建一个较为通用的业务技术架构就是如此简单
  • 2用JQuery做的一个分页效果
  • 3Java基础类String了解一下
  • 4中国民航飞行学院人事薪资管理系统实践案例
  • 5Java SE——线程介绍
  • 6简单的方法构建一个高可用服务端

2020腾讯云限时秒杀,爆款1核2G云服务器99元/年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062

2020阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods

推荐:Spring Security

http://www.family168.com/oa/springsecurity/html/index.html

up vote 2 down vote favorite The page having status 405 and also authentication is not working. Error from Spring Boot log o.s.web.servlet.PageNotFound : Request method 'POST' not supported Error from jsp page: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported Spring Info: - Using Spring Security 3.2.5 - Using Spring Boot App to start up server. - Spring-Java-Config Codes: SecurityConfig.java @EnableAutoConfiguration

@ComponentScan(basePackages = { 'org.myakasha.crm','org.myakasha.crm.controller','org.myakasha.crm.model'})

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired

DataSource dataSource;

@Autowired

public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {

auth.jdbcAuthentication().dataSource(dataSource)

.usersByUsernameQuery('select username,password, enabled from users where username=?')

.authoritiesByUsernameQuery('select username, role from user_roles where username=?');

}

@Override

public void configure(WebSecurity web) throws Exception {

web.ignoring().antMatchers('/resources/**');

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.antMatchers('/admin/**').access('hasRole('ROLE_ADMIN')')

.and()

.formLogin().loginPage('/login').failureUrl('/login?error').usernameParameter('username').passwordParameter('password')

.and()

.logout().logoutSuccessUrl('/login?logout')

.and()

.exceptionHandling().accessDeniedPage('/403')

.and()

.csrf();

}

}

SecurityController.java @Controller

public class SecurityController {

@RequestMapping(value = { '/welcome**' }, method = RequestMethod.GET)

public ModelAndView defaultPage() {

ModelAndView model = new ModelAndView();

model.addObject('title', 'Spring Security + Hibernate Example');

model.addObject('message', 'This is default page!');

model.setViewName('hello');

return model;

}

@RequestMapping(value = '/admin**', method = RequestMethod.GET)

public ModelAndView adminPage() {

ModelAndView model = new ModelAndView();

model.addObject('title', 'Spring Security + Hibernate Example');

model.addObject('message', 'This page is for ROLE_ADMIN only!');

model.setViewName('admin');

return model;

}

@RequestMapping(value = '/login', method = {RequestMethod.GET} )

public ModelAndView login(@RequestParam(value = 'error', required = false) String error,

@RequestParam(value = 'logout', required = false) String logout, HttpServletRequest request) {

ModelAndView model = new ModelAndView();

if (error != null) {

model.addObject('error', getErrorMessage(request, 'SPRING_SECURITY_LAST_EXCEPTION'));

}

if (logout != null) {

model.addObject('msg', 'You've been logged out successfully.');

}

model.setViewName('login');

return model;

Request method st not supported

}

// customize the error message

private String getErrorMessage(HttpServletRequest request, String key) {

Exception exception = (Exception) request.getSession().getAttribute(key);

String error = ';

if (exception instanceof BadCredentialsException) {

error = 'Invalid username and password!';

} else if (exception instanceof LockedException) {

error = exception.getMessage();

} else {

error = 'Invalid username and password!';

}

return error;

}

// for 403 access denied page

@RequestMapping(value = '/403', method = RequestMethod.GET)

public ModelAndView accesssDenied() {

ModelAndView model = new ModelAndView();

// check if user is login

Authentication auth = SecurityContextHolder.getContext().getAuthentication();

if (!(auth instanceof AnonymousAuthenticationToken)) {

UserDetails userDetail = (UserDetails) auth.getPrincipal();

System.out.println(userDetail);

model.addObject('username', userDetail.getUsername());

}

model.setViewName('403');

Request method post not supported spring security entry

return model;

}

}

WebConfig.java @EnableAutoConfiguration

@EnableWebMvc

@ComponentScan(basePackages = {'org.myakasha.crm','org.myakasha.crm.controller','org.myakasha.crm.model'})

public class WebConfig extends WebMvcConfigurerAdapter{

@Override

public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

configurer.enable();

}

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler('/resources/**').addResourceLocations('/resources/');

}

/**

* This function to replace servlet-content.xml

* Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory

**/

@Bean

public InternalResourceViewResolver getInternalResourceViewResolver() {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

viewResolver .setPrefix('/WEB-INF/views/');

viewResolver .setSuffix('.jsp');

return viewResolver ;

}

@Bean

public MessageSource messageSource() {

ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

messageSource.setBasenames('classpath:message');

messageSource.setUseCodeAsDefaultMessage(true);

messageSource.setDefaultEncoding('UTF-8');

return messageSource;

}

}

PersistenceConfig.java @Configuration

@EnableTransactionManagement

@PropertySource({ 'classpath:persistence-mysql.properties' })

@ComponentScan({ 'org.myakasha.crm' })

public class PersistenceConfig {

@Autowired

private Environment env;

public PersistenceConfig() {

super();

}

@Bean

public LocalSessionFactoryBean sessionFactory() {

final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

sessionFactory.setDataSource(restDataSource());

sessionFactory.setPackagesToScan(new String[] { 'org.myakasha.crm.model' });

sessionFactory.setHibernateProperties(hibernateProperties());

return sessionFactory;

}

@Bean

public DataSource restDataSource() {

final BasicDataSource dataSource = new BasicDataSource();

dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty('jdbc.driverClassName')));

dataSource.setUrl(Preconditions.checkNotNull(env.getProperty('jdbc.url')));

dataSource.setUsername(Preconditions.checkNotNull(env.getProperty('jdbc.user')));

dataSource.setPassword(Preconditions.checkNotNull(env.getProperty('jdbc.pass')));

return dataSource;

}

@Bean

@Autowired

Spring Security Httprequestmethodnotsupportedexception Request Method 'post' Not Supported

public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) {

final HibernateTransactionManager txManager = new HibernateTransactionManager();

txManager.setSessionFactory(sessionFactory);

return txManager;

}

@Bean

public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {

return new PersistenceExceptionTranslationPostProcessor();

}

final Properties hibernateProperties() {

final Properties hibernateProperties = new Properties();

hibernateProperties.setProperty('hibernate.hbm2ddl.auto', env.getProperty('hibernate.hbm2ddl.auto'));

hibernateProperties.setProperty('hibernate.dialect', env.getProperty('hibernate.dialect'));

hibernateProperties.setProperty('hibernate.show_sql', 'true');

// hibernateProperties.setProperty('hibernate.format_sql', 'true');

// hibernateProperties.setProperty('hibernate.globally_quoted_identifiers', 'true');

return hibernateProperties;

}

}

spring spring-mvc spring-security spring-java-config
|
this question edited Oct 10 '14 at 15:25 asked Oct 10 '14 at 14:55 Eliz Liew 11 1 3 1 what request are you making that fails? all your annotations say method = RequestMethod.GET ... and the error suggests you're making a PosT – Birgit Martinelle Oct 10 '14 at 15:03 Trying to login from login form, it should be leading to login?error page. – Eliz Liew Oct 10 '14 at 15:21 how do you call that login? are you submitting a form with action='post'? – Birgit Martinelle Oct 10 '14 at 15:23 Yes you are right. – Eliz Liew Oct 10 '14 at 15:28 so change your @RequestMapping(value = '/login', method = {RequestMethod.GET} ) to @RequestMapping(value = '/login', method = {RequestMethod.POST} ) – Birgit Martinelle Oct 10 '14 at 15:30

| 2 Answers

up vote 1 down vote try to add login-processing-url to the SecurityConfig in XML it looks like this

login-page='/login'

login-processing-url='/login_process'

default-target-url='/home'

authentication-failure-url='/login?error'

username-parameter='username'

password-parameter='password' />


|
this answer answered Aug 6 '15 at 8:40 MAFH 11 1

| up vote 0 down vote it seems to be that you have enabled the CSRF. can you check whether you are passing the CSRF token, in the form. if not please add the following line to your form.
Request

}

// customize the error message

private String getErrorMessage(HttpServletRequest request, String key) {

Exception exception = (Exception) request.getSession().getAttribute(key);

String error = ';

if (exception instanceof BadCredentialsException) {

error = 'Invalid username and password!';

} else if (exception instanceof LockedException) {

error = exception.getMessage();

} else {

error = 'Invalid username and password!';

}

return error;

}

// for 403 access denied page

@RequestMapping(value = '/403', method = RequestMethod.GET)

public ModelAndView accesssDenied() {

ModelAndView model = new ModelAndView();

// check if user is login

Authentication auth = SecurityContextHolder.getContext().getAuthentication();

if (!(auth instanceof AnonymousAuthenticationToken)) {

UserDetails userDetail = (UserDetails) auth.getPrincipal();

System.out.println(userDetail);

model.addObject('username', userDetail.getUsername());

}

model.setViewName('403');

return model;

}

}

WebConfig.java @EnableAutoConfiguration

@EnableWebMvc

@ComponentScan(basePackages = {'org.myakasha.crm','org.myakasha.crm.controller','org.myakasha.crm.model'})

public class WebConfig extends WebMvcConfigurerAdapter{

@Override

public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

configurer.enable();

}

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler('/resources/**').addResourceLocations('/resources/');

}

/**

* This function to replace servlet-content.xml

* Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory

**/

@Bean

public InternalResourceViewResolver getInternalResourceViewResolver() {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

viewResolver .setPrefix('/WEB-INF/views/');

viewResolver .setSuffix('.jsp');

return viewResolver ;

}

@Bean

public MessageSource messageSource() {

ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

messageSource.setBasenames('classpath:message');

messageSource.setUseCodeAsDefaultMessage(true);

messageSource.setDefaultEncoding('UTF-8');

return messageSource;

}

}

PersistenceConfig.java @Configuration

@EnableTransactionManagement

@PropertySource({ 'classpath:persistence-mysql.properties' })

@ComponentScan({ 'org.myakasha.crm' })

public class PersistenceConfig {

@Autowired

private Environment env;

public PersistenceConfig() {

super();

}

@Bean

public LocalSessionFactoryBean sessionFactory() {

final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

sessionFactory.setDataSource(restDataSource());

sessionFactory.setPackagesToScan(new String[] { 'org.myakasha.crm.model' });

sessionFactory.setHibernateProperties(hibernateProperties());

return sessionFactory;

}

@Bean

public DataSource restDataSource() {

final BasicDataSource dataSource = new BasicDataSource();

dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty('jdbc.driverClassName')));

dataSource.setUrl(Preconditions.checkNotNull(env.getProperty('jdbc.url')));

dataSource.setUsername(Preconditions.checkNotNull(env.getProperty('jdbc.user')));

dataSource.setPassword(Preconditions.checkNotNull(env.getProperty('jdbc.pass')));

return dataSource;

}

@Bean

@Autowired

Spring Security Httprequestmethodnotsupportedexception Request Method 'post' Not Supported

public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) {

final HibernateTransactionManager txManager = new HibernateTransactionManager();

txManager.setSessionFactory(sessionFactory);

return txManager;

}

@Bean

public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {

return new PersistenceExceptionTranslationPostProcessor();

}

final Properties hibernateProperties() {

final Properties hibernateProperties = new Properties();

hibernateProperties.setProperty('hibernate.hbm2ddl.auto', env.getProperty('hibernate.hbm2ddl.auto'));

hibernateProperties.setProperty('hibernate.dialect', env.getProperty('hibernate.dialect'));

hibernateProperties.setProperty('hibernate.show_sql', 'true');

// hibernateProperties.setProperty('hibernate.format_sql', 'true');

// hibernateProperties.setProperty('hibernate.globally_quoted_identifiers', 'true');

return hibernateProperties;

}

}

spring spring-mvc spring-security spring-java-config
|
this question edited Oct 10 '14 at 15:25 asked Oct 10 '14 at 14:55 Eliz Liew 11 1 3 1 what request are you making that fails? all your annotations say method = RequestMethod.GET ... and the error suggests you're making a PosT – Birgit Martinelle Oct 10 '14 at 15:03 Trying to login from login form, it should be leading to login?error page. – Eliz Liew Oct 10 '14 at 15:21 how do you call that login? are you submitting a form with action='post'? – Birgit Martinelle Oct 10 '14 at 15:23 Yes you are right. – Eliz Liew Oct 10 '14 at 15:28 so change your @RequestMapping(value = '/login', method = {RequestMethod.GET} ) to @RequestMapping(value = '/login', method = {RequestMethod.POST} ) – Birgit Martinelle Oct 10 '14 at 15:30

| 2 Answers

up vote 1 down vote try to add login-processing-url to the SecurityConfig in XML it looks like this

login-page='/login'

login-processing-url='/login_process'

default-target-url='/home'

authentication-failure-url='/login?error'

username-parameter='username'

password-parameter='password' />


|
this answer answered Aug 6 '15 at 8:40 MAFH 11 1

| up vote 0 down vote it seems to be that you have enabled the CSRF. can you check whether you are passing the CSRF token, in the form. if not please add the following line to your form.


|
this answer answered Jun 17 '16 at 13:33 Chathuranga Tennakoon 470 1 4 13

|

推荐:spring security

Request Method Post Not Supported Spring Security Login

spring security http://ryanflyer.iteye.com/blog/973319 http://blog.csdn.net/k10509806/article/details/6369131

up vote 2 down vote favorite The page having status 405 and also authentication is not working. Error from Spring Boot log o.s.web.servlet.PageNotFound : Request method 'POST' not
  • 1使用Spring Security实现权限管理
  • 2Spring Security 与 Oauth2 整合 步骤
  • 3Spring 3之MVC & Security简单整合开发(二)
  • 4spring session入门
  • 5spring security控制权限的几种方法
  • 1微信公众号文章采集,并发布到WordPress
  • 2Spring Security
  • 3spring security

最新文章

Request Method 'post' Not Supported Spring Security

  • 1在阿里架构师眼中构建一个较为通用的业务技术架构就是如此简单
  • 2Maven + Spring Boot + Mybatis 框架整合
  • 3一分钟学会spring注解之@Lazy注解
  • 4Spring Cloud构建分布式微服务云架构
  • 5spring boot之从零开始开发自己的网站
  • 6最近有人说我欺骗消费者,今天来一波视频分享




broken image