java – Spring安全 – 为什么我无法登录我的应用程序?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Spring安全 – 为什么我无法登录我的应用程序?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5258字,纯文字阅读大概需要8分钟。
内容图文
![java – Spring安全 – 为什么我无法登录我的应用程序?](/upload/InfoBanner/zyjiaocheng/781/b1161d6f6bc4443e9ed4576eb0511acc.jpg)
我在尝试登录我的应用程序时遇到了一些令人尴尬的问题.
我的Spring安全保护void configure(HttpSecurity http)抛出异常定义为:
protected void configure(HttpSecurity http) throws Exception {
System.out.println(http);
http
.formLogin()
.loginPage("/login")
.usernameParameter("ssoId")
.passwordParameter("password")
.and()
.authorizeRequests()
// I admit that this section needs some work
.antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('USER')")
.antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('USER')")
.antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('ADMIN')")
.antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('ADMIN')")
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
.and()
.rememberMe().rememberMeParameter("remember-me").tokenRepository(persistentTokenRepository()).tokenValiditySeconds(86400)
.and()
.csrf()
.and()
.exceptionHandling().accessDeniedPage("/accessDenied");
}
应用程序很好地加载并转到/ login页面.但是当我尝试使用用户主机登录并提供正确的密码时,它只会返回到/ login页面.
我的登录控制器是:
@Controller
public class LoginController {
@Autowired
UserProfileService userProfileService;
@Autowired
UserService userService;
@RequestMapping(value = { "/", "/home", "/welcome" }, method = RequestMethod.GET)
public String index(Principal principal) {
return principal != null ? "home/homeSignedIn" : "home/homeNotSignedIn";
}
@RequestMapping(value = "/login")
public String loginPage() {
return "login";
}
@RequestMapping(value="/logout", method = RequestMethod.GET)
public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return "redirect:/login?logout";
}
private String getPrincipal(){
String userName = null;
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
userName = ((UserDetails)principal).getUsername();
} else {
userName = principal.toString();
}
return userName;
}
@ModelAttribute("roles")
public List<UserProfile> initializeProfiles() {
return userProfileService.findAll();
}
}
用户
@Entity
@Table(name="user")
public class User extends BasePojo {
@NotEmpty
@Column(name="sso_id", unique=true, nullable=false)
private String ssoId;
@NotEmpty
@Column(name="password", nullable=false)
private String password;
@NotEmpty
@Column(name="first_name")
private String firstName;
@NotEmpty
@Column(name="last_name")
private String lastName;
@Column(name="email", nullable=false)
private String email;
@Column(name="state", nullable=false)
private String state=UserState.ACTIVE.getState();
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@JoinTable(name = "hrm_user_user_profile",
joinColumns = { @JoinColumn(name = "id_user", referencedColumnName="id") },
inverseJoinColumns = { @JoinColumn(name = "id_profile", referencedColumnName="id") })
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Set<UserProfile> userProfiles;
用户资料:
@Entity
@Table(name="user_profile")
public class UserProfile extends BasePojo {
private static final long serialVersionUID = 1L;
@Column(name="type", length=15, unique=true, nullable=false)
private String type = UserProfileType.USER.getUserProfileType();
// Constructor used only for initial data loading, not used after
public UserProfile() {
}
// Constructor used only for initial data loading, not used after
public UserProfile(String type) {
super();
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
UserState:
public enum UserState {
LOCKED("state.locked"),
INACTIVE("state.inactive"),
ACTIVE("state.active");
String state;
private UserState(final String state){
this.state = state;
}
public String getState(){
return state;
}
我迷失在这里.我可以得到一些帮助吗?
解决方法:
我相信您正在让用户正确进行身份验证.但是,您的/ login POST处理程序方法需要确保后续请求的标头中包含cookie或令牌.
这样,您将应用如下设置的规则
.antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('USER')")
.antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('USER')")
.antMatchers("/", "/home/*", "/alert/*", "/scheduler/*", "/agent/*", "/ftp/*", "/smtp/*", "/sql/*").access("hasRole('ADMIN')")
.antMatchers("/benefit/*", "/client/*", "/contract/*", "/role/*", "/structure/*", "/term/*").access("hasRole('ADMIN')")
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
例如,假设用户正在尝试访问/ home.由于用户未经过身份验证,因此会将其重定向到登录页面.用户输入他的用户名和密码,现在进行身份验证.完成此操作后,默认情况下,spring security会将用户重定向到返回URL / home.默认情况下,Spring安全性会将cookie添加到响应中,以便每个后续请求都在请求中包含该cookie.我相信在你的情况下不会发生这种情况.我想更多地了解您的弹簧安全配置.用户身份验证是如何进行的?它是内存认证吗?或数据源身份验证?
内容总结
以上是互联网集市为您收集整理的java – Spring安全 – 为什么我无法登录我的应用程序?全部内容,希望文章能够帮你解决java – Spring安全 – 为什么我无法登录我的应用程序?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。