24
24
import org .dromara .maxkey .authn .LoginCredential ;
25
25
import org .dromara .maxkey .authn .provider .AbstractAuthenticationProvider ;
26
26
import org .dromara .maxkey .authn .web .AuthorizationUtils ;
27
- import org .dromara .maxkey .authz .cas .endpoint .response .ServiceResponseBuilder ;
28
27
import org .dromara .maxkey .authz .cas .endpoint .ticket .CasConstants ;
29
28
import org .dromara .maxkey .authz .cas .endpoint .ticket .ServiceTicketImpl ;
30
29
import org .dromara .maxkey .authz .cas .endpoint .ticket .TicketGrantingTicketImpl ;
31
- import org .dromara .maxkey .entity .UserInfo ;
32
30
import org .dromara .maxkey .entity .apps .AppsCasDetails ;
33
31
import org .dromara .maxkey .util .StringUtils ;
34
- import org .dromara .maxkey .web .HttpResponseConstants ;
35
32
import org .slf4j .Logger ;
36
33
import org .slf4j .LoggerFactory ;
37
34
import org .springframework .beans .factory .annotation .Autowired ;
44
41
import org .springframework .security .core .Authentication ;
45
42
import org .springframework .security .core .AuthenticationException ;
46
43
import org .springframework .stereotype .Controller ;
44
+ import org .springframework .web .bind .annotation .DeleteMapping ;
45
+ import org .springframework .web .bind .annotation .GetMapping ;
47
46
import org .springframework .web .bind .annotation .PathVariable ;
48
- import org .springframework .web .bind .annotation .RequestMapping ;
49
- import org .springframework .web .bind .annotation .RequestMethod ;
47
+ import org .springframework .web .bind .annotation .PostMapping ;
50
48
import org .springframework .web .bind .annotation .RequestParam ;
51
49
52
50
import io .swagger .v3 .oas .annotations .Operation ;
61
59
@ Tag (name = "2-4-CAS REST API文档模块" )
62
60
@ Controller
63
61
public class CasRestV1Endpoint extends CasBaseAuthorizeEndpoint {
64
- final static Logger _logger = LoggerFactory .getLogger (CasRestV1Endpoint .class );
62
+ static final Logger _logger = LoggerFactory .getLogger (CasRestV1Endpoint .class );
65
63
66
64
@ Autowired
67
65
@ Qualifier ("authenticationProvider" )
68
66
AbstractAuthenticationProvider authenticationProvider ;
69
67
70
68
@ Operation (summary = "CAS REST认证接口" , description = "通过用户名密码获取TGT" ,method ="POST" )
71
- @ RequestMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 ,
72
- method =RequestMethod .POST ,
73
- consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE )
69
+ @ PostMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 ,
70
+ consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE )
74
71
public ResponseEntity <String > casLoginRestTickets (
75
72
HttpServletRequest request ,
76
73
HttpServletResponse response ,
@@ -104,21 +101,19 @@ public ResponseEntity<String> casLoginRestTickets(
104
101
_logger .error ("BadCredentialsException " , e );
105
102
return new ResponseEntity <>(e .getMessage (), HttpStatus .BAD_REQUEST );
106
103
} catch (final Exception e ) {
107
-
108
104
_logger .error ("Exception " , e );
109
105
return new ResponseEntity <>(e .getMessage (), HttpStatus .INTERNAL_SERVER_ERROR );
110
106
}
111
107
}
112
108
113
109
@ Operation (summary = "CAS REST认证接口" , description = "通过TGT获取ST" ,method ="POST" )
114
- @ RequestMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 +"/{ticketGrantingTicket}" ,
115
- method =RequestMethod .POST ,
110
+ @ PostMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 +"/{ticketGrantingTicket}" ,
116
111
consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE )
117
112
public ResponseEntity <String > requestServiceTicket (
118
113
HttpServletRequest request ,
119
114
HttpServletResponse response ,
120
115
@ PathVariable ("ticketGrantingTicket" ) String ticketGrantingTicket ,
121
- @ RequestParam (value =CasConstants .PARAMETER .SERVICE , required = false ) String casService ,
116
+ @ RequestParam (value =CasConstants .PARAMETER .SERVICE ) String casService ,
122
117
@ RequestParam (value =CasConstants .PARAMETER .RENEW ,required =false ) String renew ,
123
118
@ RequestParam (value =CasConstants .PARAMETER .REST_USERNAME ,required =false ) String username ,
124
119
@ RequestParam (value =CasConstants .PARAMETER .REST_PASSWORD ,required =false ) String password ){
@@ -138,8 +133,7 @@ public ResponseEntity<String> requestServiceTicket(
138
133
return new ResponseEntity <>("" , HttpStatus .BAD_REQUEST );
139
134
}
140
135
@ Operation (summary = "CAS REST认证接口" , description = "检查TGT状态" ,method ="GET" )
141
- @ RequestMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 + "/{ticketGrantingTicket}" ,
142
- method =RequestMethod .GET )
136
+ @ GetMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 + "/{ticketGrantingTicket}" )
143
137
public ResponseEntity <String > verifyTicketGrantingTicketStatus (
144
138
@ PathVariable ("ticketGrantingTicket" ) String ticketGrantingTicket ,
145
139
HttpServletRequest request ,
@@ -157,8 +151,7 @@ public ResponseEntity<String> verifyTicketGrantingTicketStatus(
157
151
}
158
152
159
153
@ Operation (summary = "CAS REST认证接口" , description = "注销TGT状态" ,method ="DELETE" )
160
- @ RequestMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 +"/{ticketGrantingTicket}" ,
161
- method =RequestMethod .DELETE )
154
+ @ DeleteMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 +"/{ticketGrantingTicket}" )
162
155
public ResponseEntity <String > destroyTicketGrantingTicket (
163
156
@ PathVariable ("ticketGrantingTicket" ) String ticketGrantingTicket ,
164
157
HttpServletRequest request ,
@@ -174,62 +167,5 @@ public ResponseEntity<String> destroyTicketGrantingTicket(
174
167
}
175
168
return new ResponseEntity <>("" , HttpStatus .NOT_FOUND );
176
169
}
177
-
178
- @ Operation (summary = "CAS REST认证接口" , description = "用户名密码登录接口" ,method ="POST" )
179
- @ RequestMapping (value =CasConstants .ENDPOINT .ENDPOINT_REST_USERS_V1 ,
180
- method =RequestMethod .POST ,
181
- consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE )
182
- public ResponseEntity <String > casLoginRestUsers (
183
- HttpServletRequest request ,
184
- HttpServletResponse response ,
185
- @ RequestParam (value =CasConstants .PARAMETER .SERVICE ,required =false ) String casService ,
186
- @ RequestParam (value =CasConstants .PARAMETER .REST_USERNAME ,required =true ) String username ,
187
- @ RequestParam (value =CasConstants .PARAMETER .REST_PASSWORD ,required =true ) String password ){
188
- try {
189
- if (password == null || password .isEmpty ()) {
190
- throw new BadCredentialsException ("No credentials are provided or extracted to authenticate the REST request" );
191
- }
192
-
193
- LoginCredential loginCredential =new LoginCredential (username ,password ,"CASREST" );
194
-
195
- authenticationProvider .authenticate (loginCredential ,false );
196
- UserInfo userInfo = AuthorizationUtils .getUserInfo ();
197
- TicketGrantingTicketImpl ticketGrantingTicket =new TicketGrantingTicketImpl ("Random" ,AuthorizationUtils .getAuthentication (),null );
198
-
199
- String ticket =casTicketGrantingTicketServices .createTicket (ticketGrantingTicket );
200
- String location = applicationConfig .getServerPrefix () + CasConstants .ENDPOINT .ENDPOINT_REST_TICKET_V1 + ticket ;
201
- HttpHeaders headers = new HttpHeaders ();
202
- headers .add ("location" , location );
203
- ServiceResponseBuilder serviceResponseBuilder =new ServiceResponseBuilder ();
204
- serviceResponseBuilder .setFormat (HttpResponseConstants .FORMAT_TYPE .JSON );
205
- //for user
206
- serviceResponseBuilder .setAttribute ("userId" , userInfo .getId ());
207
- serviceResponseBuilder .setAttribute ("displayName" ,userInfo .getDisplayName ());
208
- serviceResponseBuilder .setAttribute ("firstName" , userInfo .getGivenName ());
209
- serviceResponseBuilder .setAttribute ("lastname" , userInfo .getFamilyName ());
210
- serviceResponseBuilder .setAttribute ("mobile" , userInfo .getMobile ());
211
- serviceResponseBuilder .setAttribute ("birthday" , userInfo .getBirthDate ());
212
- serviceResponseBuilder .setAttribute ("gender" , userInfo .getGender ()+"" );
213
-
214
- //for work
215
- serviceResponseBuilder .setAttribute ("employeeNumber" , userInfo .getEmployeeNumber ());
216
- serviceResponseBuilder .setAttribute ("title" , userInfo .getJobTitle ());
217
- serviceResponseBuilder .setAttribute ("email" , userInfo .getWorkEmail ());
218
- serviceResponseBuilder .setAttribute ("department" , userInfo .getDepartment ());
219
- serviceResponseBuilder .setAttribute ("departmentId" , userInfo .getDepartmentId ());
220
- serviceResponseBuilder .setAttribute ("workRegion" ,userInfo .getWorkRegion ());
221
-
222
- serviceResponseBuilder .success ().setUser (userInfo .getUsername ());
223
-
224
- return new ResponseEntity <>(serviceResponseBuilder .serviceResponseBuilder (), headers ,HttpStatus .OK );
225
- } catch (final AuthenticationException e ) {
226
- _logger .error ("BadCredentialsException " , e );
227
- return new ResponseEntity <>(e .getMessage (), HttpStatus .BAD_REQUEST );
228
- } catch (final Exception e ) {
229
-
230
- _logger .error ("Exception " , e );
231
- return new ResponseEntity <>(e .getMessage (), HttpStatus .INTERNAL_SERVER_ERROR );
232
- }
233
- }
234
170
235
171
}
0 commit comments