本文整理匯總了Java中org.springframework.security.core.context.SecurityContext.setAuthentication方法的典型用法代碼示例。如果您正苦於以下問題:Java SecurityContext.setAuthentication方法的具體用法?Java SecurityContext.setAuthentication怎麽用?Java SecurityContext.setAuthentication使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.security.core.context.SecurityContext
的用法示例。
在下文中一共展示了SecurityContext.setAuthentication方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createSecurityContext
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
public SecurityContext createSecurityContext(pingis.entities.User customUser) {
SecurityContext context = SecurityContextHolder.createEmptyContext();
TmcUserDto principal = new TmcUserDto(Long.toString(customUser.getId()),
customUser.getName(),
TEST_EMAIL,
customUser.isAdministrator());
Authentication authentication = new UsernamePasswordAuthenticationToken(principal,
"password",
principal.getAuthorities());
context.setAuthentication(authentication);
return context;
}
示例2: login
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@ApiOperation(value = "Login")
@RequestMapping(value = "/login", method = RequestMethod.POST)
public boolean login(@RequestBody User user, HttpServletRequest request) {
User u = this.userService.login(user.getUsername(), user.getPassword());
if (u != null) {
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
user.getUsername(), user.getPassword());
// Authenticate the user
Authentication authentication = authenticationManager.authenticate(authRequest);
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(authentication);
// Create a new session and add the security context.
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
return true;
}
return false;
}
示例3: auth
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
public static Authentication auth(AuthenticationManager authenticationManager, String userName, String password) {
SecurityContext sc = SecurityContextHolder.getContext();
UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(userName, password);
Authentication authentication = authenticationManager.authenticate(upToken);
sc.setAuthentication(authentication);
return sc.getAuthentication();
}
示例4: testgetCurrentUserLogin
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testgetCurrentUserLogin() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
SecurityContextHolder.setContext(securityContext);
String login = SecurityUtils.getCurrentUserLogin();
assertThat(login).isEqualTo("admin");
}
示例5: testgetCurrentUserJWT
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testgetCurrentUserJWT() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "token"));
SecurityContextHolder.setContext(securityContext);
Optional<String> jwt = SecurityUtils.getCurrentUserJWT();
assertThat(jwt).contains("token");
}
示例6: testAnonymousIsNotAuthenticated
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testAnonymousIsNotAuthenticated() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
Collection<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS));
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("anonymous", "anonymous", authorities));
SecurityContextHolder.setContext(securityContext);
boolean isAuthenticated = SecurityUtils.isAuthenticated();
assertThat(isAuthenticated).isFalse();
}
示例7: testIsCurrentUserInRole
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testIsCurrentUserInRole() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
Collection<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
SecurityContextHolder.setContext(securityContext);
assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.USER)).isTrue();
assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)).isFalse();
}
示例8: testCurrentUserInRole
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testCurrentUserInRole() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
Collection<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
SecurityContextHolder.setContext(securityContext);
boolean isInRole = SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.USER);
assertThat(isInRole).isFalse();
}
示例9: testIsAuthenticated
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testIsAuthenticated() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
SecurityContextHolder.setContext(securityContext);
boolean isAuthenticated = SecurityUtils.isAuthenticated();
assertThat(isAuthenticated).isTrue();
}