当前位置: 首页 > 知识库问答 >
问题:

Spring XML到JAVA配置

宗鸿博
2023-03-14
<bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>

<security:http use-expressions="false" entry-point-ref="loginEntryPoint">
    <security:custom-filter ref="customFormLoginFilter" position="FORM_LOGIN_FILTER"/>      
    <security:logout logout-url="/logout" logout-success-url="/login?logout=true"/>

    <security:intercept-url pattern="/appointments/*" access="ROLE_USER"/>
    <security:intercept-url pattern="/schedule/*" access="ROLE_FOO"/>
    <security:intercept-url pattern="/**" access="ROLE_ANONYMOUS, ROLE_USER"/>
</security:http>

<bean id="customFormLoginFilter" class="com.fetn.security.CustomAuthenticationFilter">
    <property name="filterProcessesUrl" value="/login"/>
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="usernameParameter" value="custom_username"/> 
    <property name="passwordParameter" value="custom_password"/> 
    <property name="authenticationSuccessHandler"> 
        <bean class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> 
            <property name="defaultTargetUrl" value="/"/> 
        </bean> 
    </property> 
    <property name="authenticationFailureHandler"> 
        <bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> 
            <property name="defaultFailureUrl" value="/login/failure?error=true"/>
        </bean> 
    </property> 
</bean>

<bean id="loginEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
    <constructor-arg value="/login"/>
</bean>

<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="customAuthenticationProvider"/>
</security:authentication-manager>

我在下面写了Java Config Code,但用于注销和. antMatcher("/约会/"). access("hasRole('USER')")和antMatcher("/计划/"). access("hasRole('ADMIN')")

URL是否总是转到/login/failure?错误=true

什么是合适的java cofig代码。请帮助......

@Configuration
 @EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
private AutoUserRepository autoUserRepository;

@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {


        auth.authenticationProvider(customAuthenticationProvider);

    }



@Override
protected void configure(HttpSecurity http) throws Exception {





     http.authorizeRequests()

        .antMatchers("/appointments/*").access("hasRole('USER')").

        antMatchers("/schedule/*").access("hasRole('ADMIN')").and().exceptionHandling().authenticationEntryPoint(loginEntryPoint()).and().addFilterBefore(customFormLoginFilter(), UsernamePasswordAuthenticationFilter.class);

        http.logout().logoutUrl("/logout")
        .logoutSuccessUrl("/login?logout=true");





}


@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/**");
}



@Bean
 public DefaultWebSecurityExpressionHandler  defaultWebSecurityExpressionHandler(){

     return new DefaultWebSecurityExpressionHandler();
 }



    @Bean
    public LoginUrlAuthenticationEntryPoint loginEntryPoint(){

        LoginUrlAuthenticationEntryPoint ent=new LoginUrlAuthenticationEntryPoint("/login");

        return ent;


    }


    @Bean
    public CustomAuthenticationFilter customFormLoginFilter() throws Exception{

        CustomAuthenticationFilter filter=new CustomAuthenticationFilter();

        //setting up super class property AbstractAuthenticationProcessingFilter
        filter.setFilterProcessesUrl("/login");//login url
        filter.setAuthenticationManager(authenticationManagerBean());
        filter.setUsernameParameter("custom_username");
        filter.setPasswordParameter("custom_username");
        filter.setAuthenticationSuccessHandler(savedRequestAwareAuthenticationSuccessHandler());
        filter.setAuthenticationFailureHandler(simpleUrlAuthenticationFailureHandler());


        return filter;

    }



    @Bean
    public SavedRequestAwareAuthenticationSuccessHandler savedRequestAwareAuthenticationSuccessHandler(){

        SavedRequestAwareAuthenticationSuccessHandler surl=new SavedRequestAwareAuthenticationSuccessHandler();
        surl.setDefaultTargetUrl("/");//url after seuuces login

        return surl;
    }

    @Bean
    SimpleUrlAuthenticationFailureHandler simpleUrlAuthenticationFailureHandler(){
        SimpleUrlAuthenticationFailureHandler faillure=new SimpleUrlAuthenticationFailureHandler();
        faillure.setDefaultFailureUrl("/login/failure?error=true");


        return  faillure;

    }


    @Bean
       @Override
       public AuthenticationManager authenticationManagerBean() throws Exception {
           return super.authenticationManagerBean();
       }

}

共有1个答案

鲍钊
2023-03-14

可能是你必须补充的。在各种各样的蚂蚁之间?您还使用了两个http。*我想用一个就可以了。请参阅本页下面的代码。

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();
}
 类似资料:
  • 我怎么能改变这个xml配置: 到目前为止的java代码配置我有这个我只有这部分的问题: 这就是我到目前为止在java代码中所拥有的: 这一行给我这个错误:

  • 问题内容: 在春季,您可以XML配置一个bean以具有一个限定符。如果通过Java批注配置Bean,我似乎找不到如何附加限定符。那是怎么回事?我是否必须仅使用简单的旧名称? 问题答案: 如果您使用的是注释(不是基于Java的配置),则可以使用以下内容添加限定符(请参见Spring文档): 并使用以下命令连接bean(同样,请参见Spring文档):

  • 问题内容: 正则表达式后给我错误 请求字符串在哪里 任何帮助,将不胜感激。 问题答案: 尚未尝试匹配。先致电再致电。 输出:

  • 你可能会想知道系统提示您登录时登录表单从哪里来的,因为我们都没有提供任何的HTML或JSP文件。由于Spring Security的默认配置并没有明确设定一个登录页面的URL,Spring Security自动生成一个,基于这个功能被启用,使用默认URL处理登录的提交内容,登录后跳转的URL等等。 自动生成的登录页面可以方便应用的快速启动和运行,大多数应用程序都需要提供自己的登录页面。要做到这一点

  • 请如果有人可以帮助我在配置他们一步一步,给我一个详细的答案。

  • 到目前为止我们的 SecurityConfig 只包含了关于如何验证我们的用户的信息。Spring Security怎么知道我们想对所有的用户进行验证?Spring Security怎么知道我们需要支持基于表单的验证?原因是WebSecurityConfigurerAdapter在 configure(HttpSecurity http) 方法提供了一个默认的配置,看起来和下面类似: protec