前提
まだほぼ丸投げになってしまい申し訳ありません。
さて、今回spring boot でログイン中のユーザーの給与情報を画面に一覧で表示させる。給与情報をCSV出力できる仕様と部長クラス以上の方のみ編集ボタンが押せる仕様にしたい。
質問内容
部長以上の役職の者のみ編集ボタンをアクセスできる、または編集ボタンを表示される処理をしたいのですが、うまくできません。どうしたらよいでしょうか?
環境
・postgres sql
・Thymeleaf
その他必要なものがあれば追加
該当のソースコード(Employeeテーブル)
CREATE TABLE Employee( User_Id VARCHAR(8), Name VARCHAR(20) NOT NULL, Name_Kana VARCHAR(50) NOT NULL, Password VARCHAR(100) NOT NULL, Upn VARCHAR(50) NOT NULL, Birthday DATE NOT NULL, Gender INT NOT NULL, Prefecture_Code INT, Department_Code INT, Entry_Date DATE NOT NULL, Profile_Image BYTEA, CONSTRAINT pk_employee PRIMARY KEY(User_Id), CONSTRAINT fk_prefecturecode FOREIGN KEY(Prefecture_Code) REFERENCES Prefecture(Prefecture_Code), CONSTRAINT fk_departmentcode FOREIGN KEY(Department_Code) REFERENCES Department(Department_Code) );
該当のソースコード(Roleテーブル)
CREATE TABLE Role( Role_Code VARCHAR(5), Prefecture_Name VARCHAR(20) NOT NULL, CONSTRAINT pk_role PRIMARY KEY(Role_Code) );
該当のソースコード(Roleテーブルに追加した情報)
INSERT INTO Role VALUES('CEO', '代表取締役社長'); INSERT INTO Role VALUES('SMD', '専務取締役'); INSERT INTO Role VALUES('MD', '常務取締役'); INSERT INTO Role VALUES('DD', '本部長(事業部長)'); INSERT INTO Role VALUES('DM', '部長'); INSERT INTO Role VALUES('SM', '次長'); INSERT INTO Role VALUES('M', '課長'); INSERT INTO Role VALUES('D', '係長'); INSERT INTO Role VALUES('C', '主任'); INSERT INTO Role VALUES('S', '一般社員');
該当のソースコード(User_Authテーブル)
CREATE TABLE User_Auth( User_Id VARCHAR(8), Role_Code VARCHAR(5), CONSTRAINT fk_userid FOREIGN KEY(User_Id) REFERENCES Employee(User_Id), CONSTRAINT fk_rolecode FOREIGN KEY(Role_Code) REFERENCES Role(Role_Code) );
該当のソースコード(SalaryController.java)
package com.salary.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import com.salary.entity.Salary; import com.salary.service.SalaryService; /** * ユーザー情報 Controller */ @Controller public class SalaryController { @Autowired private SalaryService salaryService; // 給与画面(ログイン画面できたら)ログイン中のユーザーIDを取得 // @GetMapping("/") // public String displayList(@AuthenticationPrincipal User user, Model model) { // user.getId(); // List<Salary> salaryList = salaryService.searchAll(); // model.addAttribute("salaryList", salaryList); // return "html/salary_completion"; // } @GetMapping("/") public String displayList(Model model) { List<Salary> salaryList = new ArrayList<Salary>(); List<Salary> salaryListAll = new ArrayList<Salary>(); salaryListAll = salaryService.searchAll(); for (int i = 0; salaryListAll.size() > i; i++) { if (salaryListAll.get(i).getUser_id() == 1) { // user.getIdが1のところに来る salaryList.add(salaryListAll.get(i)); } } model.addAttribute("salaryList", salaryList); return "html/salary_completion"; } // 給与編集画面 @GetMapping("/salary_s") public String getSalary_s() { return "html/salary_s"; } // 検索&一覧 @GetMapping("/serch&list") public String getSerch() { return "html/serch&list"; } }
該当のソースコード(SalaryService.java)
@Service public class SalaryService { /** * ユーザー情報 Repository */ @Autowired private SalaryRepository salaryRepository; /** * ユーザー情報 全検索 * @return 検索結果 */ public List<Salary> searchAll() { return salaryRepository.findAll(); } /** * ユーザー情報 主キー検索 * @return 検索結果 */ public Salary findById(Integer user_id) { return salaryRepository.findById(user_id).get(); } }
該当のソースコード(SalaryRepository.java)
@Repository public interface SalaryRepository extends JpaRepository<Salary, Integer> {}
該当のソースコード(Salary.java)
package com.salary.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import lombok.Data; /** * ユーザー情報 Entity */ @Entity @Data @Table(name = "salary") public class Salary implements Serializable { /** * ユーザーID */ @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id") private Integer user_id; /** * 給与ナンバー */ @Id @Column(name = "salary_number") private String salary_number; /** *名前 */ @Column(name = "name") private String name; /** * 給与 */ @Column(name = "base_salary") private String base_salary; /** * 税 */ @Column(name = "tax") private String tax; /** * 保険料 */ @Column(name = "premium") private String premium; /** * 交通費 */ @Column(name = "carfare") private String carfare; /** * 総支給額 */ @Column(name = "gross_payment") private String gross_payment; /** * 交付日 */ @Column(name = "salary_date") private String salary_date; }
該当のソースコード(application.properties)
spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/employee_db spring.datasource.username=postgres spring.datasource.password=0814
該当のソースコード(salary_completion.html)
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> <head> <meta charset="UTF-8"> <title>給与画面</title> <link rel="stylesheet" th:href="@{/css/salary_completion.css}"></link> </head> <body> <div class="wrapper"> <div class="ground"> <div class="header">給与画面</div> <div th:object="${salaryDate}"> <div class="center clearfix"> <table class="table table-striped"> <tr> <th>ID</th> <th>給料ID</th> <th>名前</th> <th>給与</th> <th>税</th> <th>保険料</th> <th>交通費</th> <th>総支給額</th> <th>交付日</th> <th>オプション</th> </tr> <tr th:each="salary : ${salaryList}" th:object="${salary}"> <td th:text="${salary.user_id}"></td> <td th:text="${salary.salary_number}"></td> <td th:text="${salary.name}"></td> <td th:text="${salary.base_salary}"></td> <td th:text="${salary.tax}"></td> <td th:text="${salary.premium}"></td> <td th:text="${salary.carfare}"></td> <td th:text="${salary.gross_payment}"></td> <td th:text="${salary.salary_date}"></td> <td></td> </tr> </table> <form action="/salary_s" method="get"> <input sec:authorize="hasRole('ROLE_ADMIN')" type="submit" class="button1" value="編集"> </form> <form action="/salary" method="get"> <input type="submit" class="botton2" value="csv"> </form> <div class="button clearfix"> <form action="/serch&list" method="get"> <button type="submit" class="footer" th:href="@{/serch&list}">検索&一覧に戻る</button> </form> </div> </div> </div> </div> </div> </body> </html>
該当のソースコード(SecurityConfig.java)
import org.springframework.security.config.annotation.web.builders.HttpSecurity; public class SecurityConfig { // SecurityConfig.java protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/salary_completion/**").permitAll().antMatchers("/salary_completion/**") .hasAuthority("ROLE_ADMIN") // ← 【追加】 .anyRequest().authenticated(); } }
こちらの質問が複数のユーザーから「調査したこと・試したことが記載されていない質問」という指摘を受けました。