質問編集履歴
2
一部編子
test
CHANGED
File without changes
|
test
CHANGED
@@ -346,7 +346,7 @@
|
|
346
346
|
public class SecurityConfig {
|
347
347
|
// SecurityConfig.java
|
348
348
|
protected void configure(HttpSecurity http) throws Exception {
|
349
|
-
http.authorizeRequests().antMatchers("/c
|
349
|
+
http.authorizeRequests().antMatchers("/salary_completion/**").permitAll().antMatchers("/salary_completion/**")
|
350
350
|
.hasAuthority("ROLE_ADMIN") // ← 【追加】
|
351
351
|
.anyRequest().authenticated();
|
352
352
|
}
|
1
質問内容の追加とファイルの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,6 +4,9 @@
|
|
4
4
|
まだほぼ丸投げになってしまい申し訳ありません。
|
5
5
|
|
6
6
|
さて、今回spring boot でログイン中のユーザーの給与情報を画面に一覧で表示させる。給与情報をCSV出力できる仕様と部長クラス以上の方のみ編集ボタンが押せる仕様にしたい。
|
7
|
+
|
8
|
+
### 質問内容
|
9
|
+
部長以上の役職の者のみ編集ボタンをアクセスできる、または編集ボタンを表示される処理をしたいのですが、うまくできません。どうしたらよいでしょうか?
|
7
10
|
|
8
11
|
### 環境
|
9
12
|
・postgres sql
|
@@ -32,46 +35,97 @@
|
|
32
35
|
);
|
33
36
|
```
|
34
37
|
|
35
|
-
### 該当のソースコード(
|
38
|
+
### 該当のソースコード(Roleテーブル)
|
36
|
-
|
39
|
+
|
37
|
-
```ここに言語名を入力
|
40
|
+
```ここに言語名を入力
|
38
|
-
CREATE TABLE
|
41
|
+
CREATE TABLE Role(
|
39
|
-
|
42
|
+
Role_Code VARCHAR(5),
|
43
|
+
Prefecture_Name VARCHAR(20) NOT NULL,
|
44
|
+
CONSTRAINT pk_role PRIMARY KEY(Role_Code)
|
45
|
+
);
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
### 該当のソースコード(Roleテーブルに追加した情報)
|
50
|
+
|
51
|
+
```ここに言語名を入力
|
52
|
+
INSERT INTO Role VALUES('CEO', '代表取締役社長');
|
53
|
+
|
54
|
+
INSERT INTO Role VALUES('SMD', '専務取締役');
|
55
|
+
|
56
|
+
INSERT INTO Role VALUES('MD', '常務取締役');
|
57
|
+
|
58
|
+
INSERT INTO Role VALUES('DD', '本部長(事業部長)');
|
59
|
+
|
60
|
+
INSERT INTO Role VALUES('DM', '部長');
|
61
|
+
|
62
|
+
INSERT INTO Role VALUES('SM', '次長');
|
63
|
+
|
64
|
+
INSERT INTO Role VALUES('M', '課長');
|
65
|
+
|
66
|
+
INSERT INTO Role VALUES('D', '係長');
|
67
|
+
|
68
|
+
INSERT INTO Role VALUES('C', '主任');
|
69
|
+
|
70
|
+
INSERT INTO Role VALUES('S', '一般社員');
|
71
|
+
```
|
72
|
+
### 該当のソースコード(User_Authテーブル)
|
73
|
+
```
|
74
|
+
CREATE TABLE User_Auth(
|
40
75
|
User_Id VARCHAR(8),
|
41
|
-
|
76
|
+
Role_Code VARCHAR(5),
|
42
|
-
Base_Salary INT NOT NULL,
|
43
|
-
Tax INT NOT NULL,
|
44
|
-
Premium INT NOT NULL,
|
45
|
-
Carfare INT NOT NULL,
|
46
|
-
Gross_Payment INT NOT NULL,
|
47
|
-
Salary_Date DATE NOT NULL,
|
48
|
-
CONSTRAINT pk_salary PRIMARY KEY(Salary_Number),
|
49
|
-
CONSTRAINT fk_userid FOREIGN KEY(User_Id) REFERENCES Employee(User_Id)
|
77
|
+
CONSTRAINT fk_userid FOREIGN KEY(User_Id) REFERENCES Employee(User_Id),
|
78
|
+
CONSTRAINT fk_rolecode FOREIGN KEY(Role_Code) REFERENCES Role(Role_Code)
|
50
79
|
);
|
51
|
-
|
52
|
-
```
|
53
|
-
|
54
|
-
### 該当のソースコード(Salaryテーブルに追加した情報)
|
55
|
-
|
56
|
-
```ここに言語名を入力
|
57
|
-
INSERT INTO table_name (salary_number, base_salary,caefare,gross_payment,name,premium,salary_date,tax,user_id) VALUES (1, 20000, 200,17000,太郎,1300,6,2000,2)
|
58
80
|
```
|
59
81
|
|
60
82
|
### 該当のソースコード(SalaryController.java)
|
61
83
|
|
62
84
|
|
63
85
|
```ここに言語名を入力
|
86
|
+
package com.salary.controller;
|
87
|
+
|
88
|
+
import java.util.ArrayList;
|
89
|
+
import java.util.List;
|
90
|
+
|
91
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
92
|
+
import org.springframework.stereotype.Controller;
|
93
|
+
import org.springframework.ui.Model;
|
94
|
+
import org.springframework.web.bind.annotation.GetMapping;
|
95
|
+
|
96
|
+
import com.salary.entity.Salary;
|
97
|
+
import com.salary.service.SalaryService;
|
98
|
+
|
99
|
+
/**
|
100
|
+
* ユーザー情報 Controller
|
101
|
+
*/
|
64
102
|
@Controller
|
65
103
|
public class SalaryController {
|
66
104
|
|
67
105
|
@Autowired
|
68
106
|
private SalaryService salaryService;
|
69
107
|
|
108
|
+
// 給与画面(ログイン画面できたら)ログイン中のユーザーIDを取得
|
109
|
+
// @GetMapping("/")
|
110
|
+
// public String displayList(@AuthenticationPrincipal User user, Model model) {
|
111
|
+
// user.getId();
|
112
|
+
// List<Salary> salaryList = salaryService.searchAll();
|
113
|
+
// model.addAttribute("salaryList", salaryList);
|
114
|
+
// return "html/salary_completion";
|
70
|
-
//
|
115
|
+
// }
|
116
|
+
|
71
117
|
@GetMapping("/")
|
72
118
|
public String displayList(Model model) {
|
73
|
-
List<Salary> salaryList = salary
|
119
|
+
List<Salary> salaryList = new ArrayList<Salary>();
|
120
|
+
List<Salary> salaryListAll = new ArrayList<Salary>();
|
121
|
+
salaryListAll = salaryService.searchAll();
|
122
|
+
for (int i = 0; salaryListAll.size() > i; i++) {
|
123
|
+
if (salaryListAll.get(i).getUser_id() == 1) { // user.getIdが1のところに来る
|
124
|
+
salaryList.add(salaryListAll.get(i));
|
125
|
+
}
|
126
|
+
}
|
74
127
|
model.addAttribute("salaryList", salaryList);
|
128
|
+
|
75
129
|
return "html/salary_completion";
|
76
130
|
}
|
77
131
|
|
@@ -86,11 +140,6 @@
|
|
86
140
|
public String getSerch() {
|
87
141
|
return "html/serch&list";
|
88
142
|
}
|
89
|
-
//@AuthenticationPrincipalアノテーションを付けたUserDetailsクラスの引数を設定することで、ここにログインしているユーザー情報が入ってくる。
|
90
|
-
@GetMapping("/")
|
91
|
-
public ResponseEntity<employee> test1(@AuthenticationPrincipal UserDetails employee) {
|
92
|
-
return ResponseEntity.ok().body(new employee(employee.user_id));
|
93
|
-
}
|
94
143
|
|
95
144
|
}
|
96
145
|
```
|
@@ -118,11 +167,13 @@
|
|
118
167
|
/**
|
119
168
|
* ユーザー情報 主キー検索
|
120
169
|
* @return 検索結果
|
121
|
-
*/ public Salary findById(
|
170
|
+
*/ public Salary findById(Integer user_id) {
|
122
171
|
return salaryRepository.findById(user_id).get();
|
123
172
|
}
|
124
173
|
|
125
174
|
}
|
175
|
+
|
176
|
+
|
126
177
|
```
|
127
178
|
|
128
179
|
### 該当のソースコード(SalaryRepository.java)
|
@@ -137,38 +188,71 @@
|
|
137
188
|
|
138
189
|
|
139
190
|
```ここに言語名を入力
|
191
|
+
package com.salary.entity;
|
192
|
+
|
193
|
+
import java.io.Serializable;
|
194
|
+
|
195
|
+
import javax.persistence.Column;
|
196
|
+
import javax.persistence.Entity;
|
197
|
+
import javax.persistence.GeneratedValue;
|
198
|
+
import javax.persistence.GenerationType;
|
199
|
+
import javax.persistence.Id;
|
200
|
+
import javax.persistence.Table;
|
201
|
+
|
202
|
+
import lombok.Data;
|
203
|
+
|
204
|
+
/**
|
205
|
+
* ユーザー情報 Entity
|
206
|
+
*/
|
140
207
|
@Entity
|
141
208
|
@Data
|
142
209
|
@Table(name = "salary")
|
143
210
|
public class Salary implements Serializable {
|
144
|
-
|
211
|
+
/**
|
145
|
-
|
212
|
+
* ユーザーID
|
213
|
+
*/
|
146
214
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
147
215
|
@Column(name = "user_id")
|
148
216
|
private Integer user_id;
|
149
|
-
|
217
|
+
/**
|
150
|
-
|
218
|
+
* 給与ナンバー
|
219
|
+
*/
|
220
|
+
@Id
|
151
221
|
@Column(name = "salary_number")
|
152
222
|
private String salary_number;
|
153
|
-
|
223
|
+
/**
|
224
|
+
*名前
|
225
|
+
*/
|
154
226
|
@Column(name = "name")
|
155
227
|
private String name;
|
156
|
-
|
228
|
+
/**
|
229
|
+
* 給与
|
230
|
+
*/
|
157
231
|
@Column(name = "base_salary")
|
158
232
|
private String base_salary;
|
159
|
-
|
233
|
+
/**
|
234
|
+
* 税
|
235
|
+
*/
|
160
236
|
@Column(name = "tax")
|
161
237
|
private String tax;
|
162
|
-
|
238
|
+
/**
|
239
|
+
* 保険料
|
240
|
+
*/
|
163
241
|
@Column(name = "premium")
|
164
242
|
private String premium;
|
165
|
-
|
243
|
+
/**
|
244
|
+
* 交通費
|
245
|
+
*/
|
166
246
|
@Column(name = "carfare")
|
167
247
|
private String carfare;
|
168
|
-
|
248
|
+
/**
|
249
|
+
* 総支給額
|
250
|
+
*/
|
169
251
|
@Column(name = "gross_payment")
|
170
252
|
private String gross_payment;
|
171
|
-
|
253
|
+
/**
|
254
|
+
* 交付日
|
255
|
+
*/
|
172
256
|
@Column(name = "salary_date")
|
173
257
|
private String salary_date;
|
174
258
|
}
|
@@ -216,17 +300,18 @@
|
|
216
300
|
<th>交付日</th>
|
217
301
|
<th>オプション</th>
|
218
302
|
</tr>
|
219
|
-
<tr th:each="salary : ${salaryList}">
|
303
|
+
<tr th:each="salary : ${salaryList}" th:object="${salary}">
|
220
|
-
<td th:text="
|
304
|
+
<td th:text="${salary.user_id}"></td>
|
305
|
+
<td th:text="${salary.salary_number}"></td>
|
221
|
-
<td th:text="
|
306
|
+
<td th:text="${salary.name}"></td>
|
222
|
-
<td th:text="*{name}"></td>
|
223
|
-
<td th:text="
|
307
|
+
<td th:text="${salary.base_salary}"></td>
|
224
|
-
<td th:text="
|
308
|
+
<td th:text="${salary.tax}"></td>
|
225
|
-
<td th:text="
|
309
|
+
<td th:text="${salary.premium}"></td>
|
226
|
-
<td th:text="
|
310
|
+
<td th:text="${salary.carfare}"></td>
|
227
|
-
<td th:text="
|
311
|
+
<td th:text="${salary.gross_payment}"></td>
|
228
|
-
<td th:text="${
|
312
|
+
<td th:text="${salary.salary_date}"></td>
|
229
313
|
<td></td>
|
314
|
+
</tr>
|
230
315
|
</table>
|
231
316
|
<form action="/salary_s" method="get">
|
232
317
|
<input sec:authorize="hasRole('ROLE_ADMIN')" type="submit"
|
@@ -251,135 +336,24 @@
|
|
251
336
|
</div>
|
252
337
|
</div>
|
253
338
|
</body>
|
339
|
+
</html>
|
254
|
-
```
|
340
|
+
```
|
255
|
-
|
341
|
+
|
256
|
-
### 該当のソースコード(C
|
342
|
+
### 該当のソースコード(SecurityConfig.java)
|
257
|
-
|
258
|
-
|
259
|
-
```
|
343
|
+
```
|
260
|
-
|
344
|
+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
345
|
+
|
261
|
-
public class C
|
346
|
+
public class SecurityConfig {
|
262
|
-
private XSSFWorkbook xssfWorkbook;
|
263
|
-
private XSSFSheet xssfSheet;
|
264
|
-
private List<Salary> list;
|
265
|
-
|
266
|
-
private void headerLine() {
|
267
|
-
xssfSheet = xssfWorkbook.createSheet("All USers Details");
|
268
|
-
Row row = xssfSheet.createRow(0);
|
269
|
-
|
270
|
-
CellStyle cellStyle = xssfWorkbook.createCellStyle();
|
271
|
-
XSSFFont font = xssfWorkbook.createFont();
|
272
|
-
font.setBold(true);
|
273
|
-
font.setFontHeight(16);
|
274
|
-
cellStyle.setFont(font);
|
275
|
-
|
276
|
-
createCell(row, 0, "user_id", cellStyle);
|
277
|
-
createCell(row, 1, "salary_number", cellStyle);
|
278
|
-
createCell(row, 2, "name", cellStyle);
|
279
|
-
createCell(row, 3, "base_salary", cellStyle);
|
280
|
-
createCell(row, 4, "tax", cellStyle);
|
281
|
-
createCell(row, 5, "premium", cellStyle);
|
282
|
-
createCell(row, 6, "carfare", cellStyle);
|
283
|
-
createCell(row, 7, "gross_payment", cellStyle);
|
284
|
-
createCell(row, 8, "salary_date", cellStyle);
|
285
|
-
}
|
286
|
-
|
287
|
-
private void createCell(Row row, int countColumn, Object value, CellStyle cellStyle) {
|
288
|
-
|
289
|
-
xssfSheet.autoSizeColumn(countColumn);
|
290
|
-
Cell cell = row.createCell(countColumn);
|
291
|
-
|
347
|
+
// SecurityConfig.java
|
292
|
-
cell.setCellValue((Integer) value);
|
293
|
-
}
|
294
|
-
else if(value instanceof Boolean) {
|
295
|
-
cell.setCellValue((double) value);
|
296
|
-
}
|
297
|
-
else {
|
298
|
-
cell.setCellValue((String) value);
|
299
|
-
}
|
300
|
-
cell.setCellStyle(cellStyle);
|
301
|
-
}
|
302
|
-
|
303
|
-
private void writerDateLines() {
|
304
|
-
int rowCount = 1;
|
305
|
-
|
306
|
-
CellStyle style = xssfWorkbook.createCellStyle();
|
307
|
-
XSSFFont font = xssfWorkbook.createFont();
|
308
|
-
font.setFontHeight(18);
|
309
|
-
style.setFont(font);
|
310
|
-
|
311
|
-
for(Salary salary : list) {
|
312
|
-
Row row = xssfSheet.createRow(rowCount++);
|
313
|
-
int countColumn = 0;
|
314
|
-
|
315
|
-
createCell(row, countColumn++, salary.getUser_id(), style);
|
316
|
-
createCell(row, countColumn++, salary.getSalary_number(), style);
|
317
|
-
createCell(row, countColumn++, salary.getName(), style);
|
318
|
-
createCell(row, countColumn++, salary.getBase_salary(), style);
|
319
|
-
createCell(row, countColumn++, salary.getTax(), style);
|
320
|
-
createCell(row, countColumn++, salary.getPremium(), style);
|
321
|
-
createCell(row, countColumn++, salary.getCarfare(), style);
|
322
|
-
createCell(row, countColumn++, salary.getGross_payment(), style);
|
323
|
-
createCell(row, countColumn++, salary.getSalary_date(), style);
|
324
|
-
|
325
|
-
}
|
326
|
-
}
|
327
|
-
|
328
|
-
public CsvExporter(List<Salary>list) {
|
329
|
-
this.list = list;
|
330
|
-
xssfWorkbook = new XSSFWorkbook();
|
331
|
-
|
332
|
-
}
|
333
|
-
|
334
|
-
p
|
348
|
+
protected void configure(HttpSecurity http) throws Exception {
|
335
|
-
|
336
|
-
headerLine();
|
337
|
-
|
338
|
-
|
349
|
+
http.authorizeRequests().antMatchers("/css/**", "/js/**").permitAll().antMatchers("/admin/**")
|
339
|
-
|
340
|
-
|
350
|
+
.hasAuthority("ROLE_ADMIN") // ← 【追加】
|
341
|
-
|
351
|
+
.anyRequest().authenticated();
|
342
|
-
xssfWorkbook.close();
|
343
|
-
|
344
|
-
outputStream.close();
|
345
352
|
}
|
346
353
|
}
|
347
354
|
|
348
355
|
```
|
349
|
-
|
356
|
+
|
350
|
-
|
351
|
-
|
352
|
-
|
357
|
+
|
353
|
-
|
358
|
+
|
354
|
-
|
359
|
+
|
355
|
-
|
356
|
-
@Autowired
|
357
|
-
SalaryRepository salaryRepository;
|
358
|
-
|
359
|
-
@GetMapping("/salary")
|
360
|
-
public void exportToCsv(HttpServletResponse response) throws IOException {
|
361
|
-
|
362
|
-
response.setContentType("application/octet-stream");
|
363
|
-
|
364
|
-
// set date
|
365
|
-
DateFormat dateFormat = new SimpleDateFormat();
|
366
|
-
String currentdateandTime = dateFormat.format(new Date());
|
367
|
-
|
368
|
-
String header = "Content-disposition";
|
369
|
-
|
370
|
-
// set file name
|
371
|
-
String headerValue = "attachment; filename=allsalary_" + currentdateandTime + ".csv";
|
372
|
-
response.setHeader(header, headerValue);
|
373
|
-
|
374
|
-
// get data to list
|
375
|
-
List<Salary> salary = salaryRepository.findAll();
|
376
|
-
|
377
|
-
// insert data to file
|
378
|
-
CsvExporter csvExporter = new CsvExporter(salary);
|
379
|
-
|
380
|
-
csvExporter.exportData(response);
|
381
|
-
|
382
|
-
}
|
383
|
-
}
|
384
|
-
```
|
385
|
-
|