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

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

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

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

1232閲覧

SpringBoot ログイン機能でエラーが表示される

ai9376451

総合スコア15

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2022/05/27 03:21

Spring Boot アプリケーションに Spring Security を導入し認証・認可を実装しています。認証はユーザーIDとパスワードを使ったフォーム認証を作成したいのですが,エラーが表示されていて、なぜエラーが出てるのか分からずにいます。IntelliJを使って作成しています。

**出ているエラー** > Task :compileJava C:\Users\:::\OneDrive\�h�L�������g\poco-tech-spring-boot-54fa10ab734c2e087d7a61e9ee850b76a7377f11\poco-tech-spring-boot-54fa10ab734c2e087d7a61e9ee850b76a7377f11\src\main\java\com\example\its\web\user\UserController.java:15: �G���[: �V���{�������‚����܂��� private final UserServise userService; ^ �V���{��: �N���X UserServise �ꏊ: �N���X UserController C:\Users\:::\OneDrive\�h�L�������g\poco-tech-spring-boot-54fa10ab734c2e087d7a61e9ee850b76a7377f11\poco-tech-spring-boot-54fa10ab734c2e087d7a61e9ee850b76a7377f11\src\main\java\com\example\its\web\user\UserController.java:13: �G���[: �V���{�������‚����܂��� @RequiredArgsConstructor ^ �V���{��: �N���X UserServise �ꏊ: �N���X UserController �G���[2�� > Task :compileJava FAILED 1 actionable task: 1 executed FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 3s 8:43:14: Execution finished ':classes'.
package com.example.its.config; import lombok.RequiredArgsConstructor; 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.password.NoOpPasswordEncoder; @EnableWebSecurity @RequiredArgsConstructor public class SecurityConfig extends WebSecurityConfigurerAdapter { private final UserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests().antMatchers("/h2-console/**").permitAll() .and() .csrf().ignoringAntMatchers("/h2-console/**") .and().headers().frameOptions().disable(); http .authorizeRequests() .mvcMatchers("/login/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login"); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService) .passwordEncoder(NoOpPasswordEncoder.getInstance()); } }
package com.example.its.domain.auth; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.User; import java.util.Collection; public class CustomUserDetails extends User{ public CustomUserDetails(String username, String password, Collection<? extends GrantedAuthority>authorities){ super(username,password,authorities); } }
package com.example.its.domain.auth; import lombok.RequiredArgsConstructor; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import java.util.Collections; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class CustomUserDetailsService implements UserDetailsService { private final UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { return userRepository.findByUsername(username) .map( user -> new CustomUserDetails( user.getUsername(), user.getPassword(), Collections.emptyList() ) ) .orElseThrow( ()->new UsernameNotFoundException( "Given username is not found.(username= '"+username+"')" ) ); } }
package com.example.its.domain.auth; import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor public class User { private String username; private String password; }
package com.example.its.domain.auth; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.Optional; import java.util.List; @Mapper public interface UserRepository { @Select("select * from users where username = #{username}") Optional<User> findByUsername(String username); @Select("select * from users") List<User>findAll(); }
package com.example.its.domain.auth; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; @Service @RequiredArgsConstructor public class UserService { private final UserRepository userRepository; private List<User> findAll(){ return userRepository.findAll(); } }
package com.example.its.web.issue; import com.example.its.domain.issue.IssueService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/issues") @RequiredArgsConstructor public class IssueController { private final IssueService issueService; @GetMapping public String showList(Model model) { model.addAttribute("issueList", issueService.findAll()); return "issues/list"; } @GetMapping("/creationForm") public String showCreationForm(@ModelAttribute IssueForm form) { return "issues/creationForm"; } @PostMapping public String create(@Validated IssueForm form, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { return showCreationForm(form); } issueService.create(form.getSummary(), form.getDescription()); return "redirect:/issues"; } @GetMapping("/{issueId}") public String showDetail(@PathVariable("issueId") long issueId, Model model) { model.addAttribute("issue", issueService.findById(issueId)); return "issues/detail"; } }
package com.example.its.web.user; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import java.util.stream.Collectors; @Controller @RequestMapping("/users") @RequiredArgsConstructor public class UserController { private final UserServise userService; @GetMapping public String showList(Model model){ model.addAttribute("userList", userService.findAll()); return "users/list"; } }
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout(~{::title}, ~{::body})}"> <head> <title>課題一覧 | 課題管理アプリケーション</title> </head> <body> <h1 class="mt-3">課題一覧</h1> <a href="../index.html" th:href="@{/}">トップページ</a> <a href="./creationForm.html" th:href="@{/issues/creationForm}">作成</a> <table class="table"> <thead> <tr> <th>#</th> <th>概要</th> </tr> </thead> <tbody> <tr th:each="issue : ${issueList}"> <th th:text="${issue.id}">(id)</th> <td> <a href="./detail.html" th:href="@{/issues/{issueId}(issueId=${issue.id})}" th:text="${issue.summary}"> (summary) </a> </td> </tr> </tbody> </table> </body> </html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout(~{::title}, ~{::body})}"> <head> <title>トップページ | 課題管理アプリケーション</title> </head> <body> <h1 class="mt-3">課題管理アプリケーション</h1> <ul> <li> <a href="./issues/list.html" th:href="@{/issues}">課題一覧</a> <li><a href="./users/list.html" th:href="@{/users}">ユーザー一覧</a></li> <li><a hef="./logout.html" th:href="@{/logout}">ログアウト</a></li> </li> </ul> </body> </html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout(~{::title}, ~{::body})}"> <head> <title>ユーザー一覧 | 課題管理アプリケーション</title> </head> <body> <h1 class="mt-3">ユーザー一覧</h1> <table class="table"> <thead> <tr> <th>username</th> <th>概要</th> </tr> </thead> <tbody> <tr th:each="user : ${userList}"> <th th:text="${user.username}">(id)</th> </tr> </tbody> </table> </body> </html>

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

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

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

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

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

guest

回答1

0

ベストアンサー

文字化けしていて、エラーが見えないのですが、おそらくimportエラーです。
(UserController.javaに、UserServiceクラスのimportが無い)

java

1// UserController.java 2import com.example.its.domain.auth.UserService; //インポートを追加 3 4public class UserController { 5 // UserServiseの「se」を、UserService に変更 6 private final UserService userService;

IntelliJ IDEAの文字化けは、メニューの「ヘルプ」>「カスタムVMオプションの編集」に、以下を追加すると直ると思います。

-Dfile.encoding=UTF-8

参考URL: コンソールで日本語が文字化けしたときの解決方法

投稿2022/05/28 08:58

編集2022/06/04 02:35
KT001

総合スコア618

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問