本文整理汇总了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();
}