【发布时间】:2017-01-09 04:31:17
【问题描述】:
我使用的是 Spring 4.3.1.RELEASE 版本,它是自定义身份验证登录应用程序。但是我遇到了问题
先看代码
CustomAuthenticationProvider.java
@Component
@Qualifier(value = "customAuthenticationProvider")
public class CustomAuthenticationProvider implements AuthenticationProvider{
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = (String) authentication.getCredentials();
User user = new User();
user.setUsername(username);
user.setPassword(password);
Role r = new Role();
r.setName("ROLE_ADMIN");
List<Role> roles = new ArrayList<Role>();
roles.add(r);
Collection<? extends GrantedAuthority> authorities = roles;
return new UsernamePasswordAuthenticationToken(user, password, authorities);
}
public boolean supports(Class<?> arg0) {
return true;
}
}
SecurityConfiguration.java
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider);
}
//.csrf() is optional, enabled by default, if using WebSecurityConfigurerAdapter constructor
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_USER')")
.and()
.formLogin()
.loginPage("/login").failureUrl("/login?error")
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.csrf();
}
}
login.jsp 这是我的登录页面
<form name="loginForm" novalidate ng-submit="ctrl.login(user)">
<div class="form-group" ng-class="{'has-error': loginForm.username.$invalid}">
<input class="form-control" name="username" id="username" type="text"
placeholder="Username" required ng-model="user.username" />
<span class="help-block"
ng-show="loginForm.username.$error.required">Required</span>
</div>
<div class="form-group" ng-class="{'has-error': loginForm.password.$invalid}">
<input class="form-control" name="password" id="password" type="password"
placeholder="Password" required ng-model="user.password" />
<span class="help-block"
ng-show="loginForm.password.$error.required">Required</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary pull-right"
value="Login" title="Login" ng-disabled="!loginForm.$valid">
<span>Login</span>
</button>
</div>
</form>
在 CustomAuhtenticationProvider 类中进行 authenticate()
- authentication.getCredentials();
- authentication.getName();
两者都给出空字符串,但我需要用户名和密码。
这是我的 AngularJS 服务
Service.js
function loginUser(user) {
var config = {
headers: {
'csrf_token': csrfToken
}
}
var deferred = $q.defer();
$http.post("/login", user,config)
.then(
function (response) {
deferred.resolve(response.data);
},
function(errResponse){
console.error('Error while creating User');
deferred.reject(errResponse);
}
);
return deferred.promise;
}
【问题讨论】:
-
其实我用的是AngularJS,它包含在Service里面
-
嗯...您如何发送用户名和密码?常见的表单参数或其他任何格式如json?
-
我知道,这就是我告诉你尝试禁用 csrf() 的原因,因为表单中没有出现 csrf 令牌。正如@M.Deinum 所说,包括您的角度控制器或至少包含登录请求消息格式。我打赌你是以 json 格式发送的
-
AngularJS 默认为表单提交提供 json 格式。为什么你需要你自己的提供者,它甚至不做任何事情? bcz 我需要自己的身份验证,而不是 LDAP、DAO 或内存身份验证
-
已经是@Naman
标签: java spring spring-mvc authentication spring-security