質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Q&A

0回答

1660閲覧

SpringSecurity での「No mapping found for HTTP request with URI」(404エラー)について

tuchida

総合スコア20

Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

0グッド

0クリップ

投稿2018/12/11 09:29

編集2022/01/12 10:55

SpringMVCおよびSpringSecutiryで開発していますが、最初の画面で404が出てしまい
コントローラ―に制御がわたりません。
@EnableWebSecurityの指定がよくないのではと思うのですが指定方法がよくわかりません。
どなたかお分かりの方がいらっし````ゃればご教授おねがいいたします。
<@EnableWebSecurityのjavaクラス>

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder() ; @Override protected void configure(final HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin().loginPage("/Login.jsp").failureUrl("/Login?error").permitAll() .loginPage("/Login.jsp") .failureUrl("/Login.jsp") .defaultSuccessUrl("/Login", true) .permitAll() .and() .logout() .logoutRequestMatcher(new AntPathRequestMatcher("/Logout")) .logoutSuccessUrl("/Login") .permitAll(); } @Autowired public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService()) .passwordEncoder(passwordEncoder); } }

<コントローラクラス>

import

1 2import org.slf4j.Logger; 3import org.slf4j.LoggerFactory; 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.stereotype.Controller; 6import org.springframework.ui.Model; 7import org.springframework.web.bind.annotation.ModelAttribute; 8import org.springframework.web.bind.annotation.RequestMapping; 9import org.springframework.web.bind.annotation.RequestMethod; 10 11import VEcomm.co.jp.constant.GlobalConst; 12import co.com.VESales.Service.LoginService; 13 14@Controller 15public class LoginController { 16 17 private static final Logger logger = (Logger) LoggerFactory.getLogger(LoginController.class); 18 19 @Autowired 20 private LoginService loginService; 21 22 private String LoginFormName = "Login"; 23 private String VEMenuFormName = GlobalConst.cnsRedirect + "VEMenu"; 24 25 26 public LoginController() { 27 28 loginService = new LoginService(); 29 30 } 31 32 33 @RequestMapping(value = { "/", "Login","index","VESales"}, method = RequestMethod.GET) 34 public String LoginGet(Locale locale, Model model) { 35 36 String wRetForm; 37 38 39 logger.info("LoginGet.", locale); 40 41 // ログイン画面初期化 42 loginService.LoginFormInit(model); 43 44 wRetForm = LoginFormName; 45 46 return wRetForm; 47 48 49 } 50 @RequestMapping(value = { "/", "Login","index","VESales"}, method = RequestMethod.POST) 51 public String LoginPut(@ModelAttribute("userid") String pUserid, 52 @ModelAttribute("password") String pPassword, 53 Locale locale, Model model) { 54 String wRetForm; 55 56 logger.info("LoginPut.", locale); 57 58 if (loginService.LoginUsercheck(pUserid, model)){ 59 60 61 if (loginService.LoginPasswordChk(pUserid, pPassword, model)) { 62 63 wRetForm = VEMenuFormName; 64 65 66 }else { 67 68 wRetForm = LoginFormName; 69 70 } 71 72 73 }else { 74 75 wRetForm = LoginFormName; 76 77 } 78 79 return wRetForm; 80 81 } 82}

<web.xml>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- エラーページ --> <!-- <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page> --> <!-- 文字コード対応 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<Servret-cntext> ```<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="co.com.VESales" /> <!-- MySQL --> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <beans:property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <beans:property name="url" value="jdbc:mysql://127.0.0.1:3306/vesales" /> <beans:property name="username" value="veusr01" /> <beans:property name="password" value="nanami00" /> </beans:bean> <beans:bean class="org.springframework.jdbc.core.JdbcTemplate"> <beans:constructor-arg ref="dataSource" /> </beans:bean> <!-- アプリケーションプロパティ --> <beans:bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <beans:property name="locations"> <beans:list> <beans:value>classpath:application.properties</beans:value> </beans:list> </beans:property> <beans:property name="fileEncoding" value="UTF-8" /> </beans:bean> <beans:bean id="placeHolderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <beans:property name="location" value="classpath:application.properties" /> </beans:bean>

</beans:beans>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

A-pZ

2018/12/11 15:56

設定とソースコードは、コードブロックで1つ1つ囲いましょう。質問投稿後でも編集できます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問