gpt4 book ai didi

spring - 自定义身份验证提供程序的工作原理

转载 作者:行者123 更新时间:2023-12-04 04:38:06 27 4
gpt4 key购买 nike

美好的一天。

请帮忙。无法理解应如何触发我的自定义身份验证提供程序。

我有:

Spring 上下文.xml

<security:http pattern="/login" security="none" />  


<security:http auto-config="true" use-expressions="true">


<security:form-login login-page="/login"/>

<security:intercept-url pattern="/" access="hasRole('ROLE_USER')"/>

<security:form-login authentication-failure-url="www.google.com"/>

</security:http>



<security:authentication-manager>

<security:authentication-provider user-service-ref="userSecurityService"/>

</security:authentication-manager>


<bean id="webContentDAOImpl" class="demidov.pkg.persistence.WebContentDAOImpl">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>


<bean id="userSecurityService" class="demidov.pkg.persistence.UserSecurityService">
<property name="webContentDAOIF" >
<ref bean="webContentDAOImpl"/>
</property>
</bean>

登录 Controller :
   @Controller
public class LoginController {

@RequestMapping(value="/login", method=RequestMethod.GET)
public String login() {

return "login";
}


@RequestMapping(value="/security/j_spring_security_check", method=RequestMethod.POST)
public String access() {

return "redirect:/";

}


}

登录JSP页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="security/j_spring_security_check" method="post">

UserName: <input type="text"/> <br>
Password: <input type="password"/> <br>

<br>

<input type="submit"/>


</form>

</body>
</html>

自定义主体解析器:
   public class UserSecurityService implements UserDetailsService{


WebContentDAOIF webContentDAOIF;

public WebContentDAOIF getWebContentDAOIF() {
return webContentDAOIF;
}


public void setWebContentDAOIF(WebContentDAOIF webContentDAOIF) {
this.webContentDAOIF = webContentDAOIF;
}


@Override
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {


UserDetails userDetails = null;


TheUser theUser = webContentDAOIF.fetchUserByName(userName);

userDetails = new User(theUser.getUserEmale(), theUser.getUserPassword(), true, true, true, true, getAthorities(theUser.getRoleAccess()));


return userDetails;
}


public Collection<GrantedAuthority> getAthorities(String role) {


List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);


authList.add(new SimpleGrantedAuthority(" "));


if ( role.equals("ROLE_USER")) {

authList.add(new SimpleGrantedAuthority("ROLE_USER"));
}

// Return list of granted authorities
return authList;

}

}

我只是不明白我的自定义主体解析器应该如何与安全一起工作。它应该如何触发以及由什么触发???当我在登录页面上输入错误的用户名和密码时,它似乎不适用于我的 UserSecurityService由于我的 hasRole(ROLE_USER),我只是简单地再次将我重定向到登录页面在 spring-context.xml 中。我相信 j_spring_security_check可能会做某事,但对此表示怀疑。请帮我理解。

最佳答案

请引用下面提到的链接,可能会有所帮助:-
spring security custom authentication

关于spring - 自定义身份验证提供程序的工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380108/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com