使用ReactJS在Sprint启动中实现Spring Security

时间:2017-03-22 20:55:05

标签: java reactjs spring-boot spring-security

我正在尝试使用Spring Boot和ReactJS使用Spring Security实现用户名和密码的基本登录身份验证。我试着在网上做几个例子,但大多数都是关于JSP的,而不是ReactJS。我可以使用Spring Security的默认登录来运行它,但是当我尝试使用ReactJS进行自定义登录页面时。我无法找到办法。任何帮助或指针都非常感谢。

**login.html**



 var App = React.createClass({

    getInitialState: function () {
           return {
            username: "",
            password: ""
            };
        },
        _onSubmit: function (event) {

            event.preventDefault();

              var data = {
                    username: this.state.username,
                    password: this.state.password
                }

                $.ajax({
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json'
                    },
                    type: "POST",
                    url: "/login",
                    data: JSON.stringify(data),
                    success: function(data){

                    }.bind(this),
                    error:function(data)
                    {
                        alert(data.responseJSON.message);
                    }
                });

        },
        _onUserNameChange: function (event) {
            this.setState({username: event.target.value});
        },
        _onPasswordChange: function (event) {
            this.setState({password: event.target.value});
        },
      render: function() {
    return (
      <Grommet.App>
        <Box pad='medium' align='center' >
          <Box pad={{ vertical: 'medium', horizontal: 'medium', between: 'medium' }} align='center'>
          </Box>
          <Box size='large' pad='medium'>
            <Section align='center' pad='small' separator='top'>
              <Label margin='none' uppercase={true}>Tool</Label>
            </Section>
          </Box>
          <Form pad='medium'>
            <FormField label='User Name' error={this.state.usernameError}>
              <input type='text' value={this.state.username} onChange={this._onUserNameChange} />
            </FormField>
            <FormField label='Password' value={this.state.password} onChange={this._onPasswordChange} error={this.state.passwordError}>
              <input type='password' />
            </FormField>
            <Footer pad={{ vertical: 'medium' }} direction='column'>
              <Button label='Login' primary={true} fill={true} onClick={this._onSubmit} type="submit" name="submit" />
            </Footer>
          </Form>
        </Box>
      </Grommet.App>
    );
      }

      });

        var element = document.getElementById('content');
        ReactDOM.render(React.createElement(App), element);




 **SecurityConfig.java**


 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Override
public void configure(HttpSecurity httpSecurity) throws Exception{
    httpSecurity.authorizeRequests()
        .antMatchers("/*").hasRole("USER")
        .and()
    .formLogin()
         .loginProcessingUrl("/login.html");
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
    auth.inMemoryAuthentication()
        .withUser("user").password("password").roles("USER");

}

}

1 个答案:

答案 0 :(得分:0)

问题是登录处理URL的路径:您将凭据从ReactJS发布到 / login 网址,但是在Spring安全配置中,您正在收听 /login.html

只需将配置更改为:

 .loginProcessingUrl("/login");

我认为没关系。 祝你好运!